CF260D.Black and White Tree

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The board has got a painted tree graph, consisting of n nodes. Let us remind you that a non-directed graph is called a tree if it is connected and doesn't contain any cycles.

Each node of the graph is painted black or white in such a manner that there aren't two nodes of the same color, connected by an edge. Each edge contains its value written on it as a non-negative integer.

A bad boy Vasya came up to the board and wrote number s__v near each node v — the sum of values of all edges that are incident to this node. Then Vasya removed the edges and their values from the board.

Your task is to restore the original tree by the node colors and numbers s__v.

黑板上画有一棵由 nn 个节点构成的树(无向图)。我们提醒您:一个无向图若连通且不含环,则称为一棵树。

图中每个节点被涂成黑色或白色,使得任意一条边所连接的两个节点颜色均不相同。每条边上都写有一个非负整数作为其权值。

一个顽皮的男孩瓦夏走到黑板前,在每个节点 vv 旁写下数字 svs_v —— 即所有与该节点关联的边的权值之和。随后,瓦夏将所有边及其权值从黑板上擦除了。

您的任务是根据各节点的颜色以及数值 svs_v,还原出原始的树结构。

输入格式

The first line of the input contains a single integer n (2 ≤ n ≤ 105) — the number of nodes in the tree. Next n lines contain pairs of space-separated integers c__i, s__i (0 ≤ c__i ≤ 1, 0 ≤ s__i ≤ 109), where c__i stands for the color of the i-th vertex (0 is for white, 1 is for black), and s__i represents the sum of values of the edges that are incident to the i-th vertex of the tree that is painted on the board.

输入的第一行包含一个整数 nn(2 ≤ n ≤ 1052 \leq n \leq 10^5)——树中节点的数量。接下来的 nn 行每行包含一对用空格分隔的整数 cic_i、sis_i(0 ≤ ci ≤ 10 \leq c_i \leq 1,0 ≤ si ≤ 1090 \leq s_i \leq 10^9),其中 cic_i 表示第 ii 个顶点的颜色(00 表示白色,11 表示黑色),而 sis_i 表示画在黑板上的、与树中第 ii 个顶点相关联的所有边的权值之和。

输出格式

Print the description of n - 1 edges of the tree graph. Each description is a group of three integers v__i, u__i, w__i (1 ≤ v__i, u__i ≤ n, v__i ≠ u__i, 0 ≤ w__i ≤ 109), where v__i and u__i — are the numbers of the nodes that are connected by the i-th edge, and w__i is its value. Note that the following condition must fulfill c__v__i ≠ c__u__i.

It is guaranteed that for any input data there exists at least one graph that meets these data. If there are multiple solutions, print any of them. You are allowed to print the edges in any order. As you print the numbers, separate them with spaces.

输出树图的 n−1n-1 条边的描述。每条边的描述由三个整数 viv_i、uiu_i、wiw_i(其中 1≤vi,ui≤n1 \le v_i, u_i \le n,vi≠uiv_i \ne u_i,0≤wi≤1090 \le w_i \le 10^9)组成,分别表示第 ii 条边所连接的两个节点编号以及该边的权值。注意需满足如下条件:cvi≠cuic_{v_i} \ne c_{u_i}。

保证对于任意输入数据,均至少存在一个满足这些条件的图。若存在多个解,输出任意一个即可。边的输出顺序可以任意。输出各数字时,请用空格分隔。

输入输出样例

  • 输入#1

    3
    1 3
    1 2
    0 5

    输出#1

    3 1 3
    3 2 2
  • 输入#2

    6
    1 0
    0 3
    1 8
    0 2
    0 3
    0 0

    输出#2

    2 3 3
    5 3 3
    4 3 2
    1 6 0
    2 1 0

输入解题思路,AI测评打分。不知道怎么写?

首页