Codeforces Round #158 (Div. 2) D. Black and White Tree

D. Black and White Tree

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 sv 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 sv.

Input

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 cisi (0 ≤ ci ≤ 1, 0 ≤ si ≤ 109), where ci stands for the color of the i-th vertex (0 is for white, 1 is for black), and sirepresents the sum of values of the edges that are incident to the i-th vertex of the tree that is painted on the board.

Output

Print the description of n - 1 edges of the tree graph. Each description is a group of three integers viuiwi (1 ≤ vi, ui ≤ nvi ≠ ui, 0 ≤ wi ≤ 109), where vi and ui — are the numbers of the nodes that are connected by the i-th edge, and wi is its value. Note that the following condition must fulfill cvi ≠ cui.

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.

Examples

input
3
1 3
1 2
0 5
output
3 1 3
3 2 2
input
6
1 0
0 3
1 8
0 2
0 3
0 0
output
2 3 3
5 3 3
4 3 2
1 6 0
2 1 0

题意:有n个结点构成一棵树。两个相连的结点有着不一样的颜色(黑白)。现在告诉你每个结点的颜色和度。让你构造出一棵符合要求的树。

解法:构造

首先需要知道一个结论,由于只有两种颜色,所以黑色结点的度的sum和白色结点的度的sum是相等的。因此我们把所有的点划分为两个集合一个黑色一个白色,并对其按度从小到大进行排序。(好像、其实、大概、不用排序)

扫描二维码关注公众号,回复: 2706575 查看本文章

每一次取两个集合中最小的两个结点,取最小的原因是为了保证树能被构造出来,如果取大的可能会导致不够减(可以想一想)

每次取出来的两个结点用小的那个作为权值构造一条边,然后删去一个结点(删一个就可以)

当有其中一个集合为空时,剩下所有的结点只要连一条权值为0的边到另一种颜色的任一结点即可。

原先的代码:有点繁琐

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <math.h>
#include <queue>
#include <vector>
#include <map>
#include <climits>
#define ll long long
using namespace std;

struct pii{
    int first, second;
    pii(int f, int s)
    {
        first = f;
        second = s;
    }
    pii();
};
bool operator < (const pii &a, const pii &b)
{
    return a.first > b.first;
}
priority_queue<pii> black;
priority_queue<pii> white;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        int c, s;
        cin >> c >> s;
        if(c)
            black.push(pii(s, i));
        else
            white.push(pii(s, i));
    }
    n--;
    map<int, int> Q;
    while(n > 0)
    {
        n--;
        pii b = black.top(), w = white.top();
        black.pop();
        white.pop();
        if(n < 0) break;
        int mmin = min(b.first, w.first);
        b.first = b.first - mmin;
        w.first = w.first - mmin;
        if(b.first == 0) white.push(w);
        else if (w.first == 0) black.push(b) ;
        cout << b.second << ' ' << w.second << ' ' << mmin << endl;
        if(n == 0) break;
        if(black.empty())
        {
            while(white.size())
            {
                if(white.top().second != w.second)
                cout << white.top().second << ' ' <<  b.second << ' '  <<  0 << endl;
                white.pop();
            }
            break;
        }
        if(white.empty())
        {
            while(black.size())
            {
                if(black.top().first != b.second)
                cout << black.top().second << ' ' << w.second << ' ' << 0 << endl;
                black.pop();
            }
            break;
        }
    }
    return 0;
}

精简版后的代码(无排序)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <math.h>
#include <queue>
#include <vector>
#include <map>
#include <climits>
#define ll long long
#define pii pair<int, int>
using namespace std;
vector<pii> white;
vector<pii> black;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        int c, s;
        cin >> c >> s;
        if(c)
            black.push_back(pii(s, i));
        else
            white.push_back(pii(s, i));
    }
    n--;
    pii w, b;
    while(white.size() && black.size())
    {
        w = white.back();
        b = black.back();
        int mn = min(w.first, b.first);
        w.first -= mn;
        b.first -= mn;
        if(w.first == 0) {
            black[black.size() - 1] = b;
            white.pop_back();
        }
        else if (b.first == 0) {
            white[white.size() - 1] = w;
            black.pop_back();
        }
        cout << w.second << ' ' << b.second << ' ' << mn << endl;
    }
    if(white.size())
    {
        for(auto idx:white)
            if(idx.second != w.second)
                cout << idx.second << ' ' << b.second << ' ' << endl;
    }
    if(black.size())
    {
        for(auto idx:black)
            if(idx.second != b.second)
                cout << idx.second << ' ' << w.second << ' ' << 0 <<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sinat_37158899/article/details/81185507