Problem:
Score : 400 points

Problem Statement

We have a tree with N vertices numbered 1 to N.
The i-th edge in the tree connects Vertex u_i and Vertex v_i, and its length is w_i.
Your objective is to paint each vertex in the tree white or black (it is fine to paint all vertices the same color) so that the following condition is satisfied:

For any two vertices painted in the same color, the distance between them is an even number.

Find a coloring of the vertices that satisfies the condition and print it. It can be proved that at least one such coloring exists under the constraints of this problem.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq u_i < v_i \leq N

1 \leq w_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
u_1 v_1 w_1
u_2 v_2 w_2
.
.
.
u_{N - 1} v_{N - 1} w_{N - 1}

Output

Print a coloring of the vertices that satisfies the condition, in N lines.
The i-th line should contain 0 if Vertex i is painted white and 1 if it is painted black.

If there are multiple colorings that satisfy the condition, any of them will be accepted.

Sample Input 1

3
1 2 2
2 3 1

Sample Output 1

0
0
1

Sample Input 2

5
2 5 2
2 3 10
1 3 8
3 4 2

Sample Output 2

1
0
1
0
1

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;

typedef pair<int, int> P;
typedef pair<long long, long long> LLP;

/*
  UnionFind Tree
*/
class UnionFind {
    private: 
        vector<int> par;
        vector<int> rank;
        vector<int> cnt;
    
    public:
        UnionFind(int n);
        int find(int x);
        void unite(int x, int y);
        bool same(int x, int y);
        int count(int x);
        void printStatus();
};

UnionFind::UnionFind(int n) {
    for (int i = 0; i < n; i++) {
        par.push_back(i);
        rank.push_back(0);
        cnt.push_back(1);
    }
}
int UnionFind::find(int x) {
    if (par[x] == x) return x;
    else return par[x] = find(par[x]);
}
void UnionFind::unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y]) {
        cnt[y] += cnt[x];
        par[x] = y;
    } else {
        cnt[x] += cnt[y];
        par[y] = x;
        if (rank[x] == rank[y]) rank[x]++;
    }
}
bool UnionFind::same(int x, int y) {
    return find(x) == find(y);
}
int UnionFind::count(int x) {
    return cnt[find(x)];
}
void UnionFind::printStatus() {
    printf("----------------\n");
    for (int i = 0; i < par.size(); i++) {
        printf("[node %d]: parent = %d, rank = %d, count = %d\n", i, par[i], rank[i], cnt[i]);
    }
}

int main() {
    int N;
    cin >> N;
    UnionFind tree(N);
    vector<int> u, v, w;
    bool isparent[N];
    int parentScore[N];
    for (int i = 0; i < N; i++) {
        isparent[i] = false;
        parentScore[i] = 0;
    }
    for (int i = 0; i < N - 1; i++) {
        int ut, vt, wt;
        scanf("%d %d %d", &ut, &vt, &wt);
        u.push_back(ut - 1);
        v.push_back(vt - 1);
        w.push_back(wt);
        if (wt % 2 == 0) tree.unite(ut - 1, vt - 1);
    }
    vector<int> pairs[N]; 
    for (int i = 0; i < N - 1; i++) {
        isparent[tree.find(i)] = true;
        if (w[i] % 2 == 1) {
            int j, k;
            j = tree.find(u[i]);
            k = tree.find(v[i]);
            if (j < k) {
                pairs[j].push_back(k);
            } else {
                pairs[k].push_back(j);
            }
        }
    }
    isparent[tree.find(N - 1)] = true;
    queue<int> que;
    int l = 0;
    int count = 1;
    while (!isparent[l]) l++;
    que.push(l);
    while (!que.empty()) {
        l = que.front();
        que.pop();
        for (int i = 0; i < pairs[l].size(); i++) {
            parentScore[pairs[l][i]] = parentScore[l] + 1;
            que.push(pairs[l][i]);
        }
    }

    for (int i = 0; i < N; i++) {
        cout << (parentScore[tree.find(i)] % 2) << endl;
    }

    return 0;
}