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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

typedef struct Node {
    int num;
    int mem;
    int *dis;
    struct Node **next;
    bool looked;
} node;

node *make_node(int);
void add_next(node *, node *, int);
node *build_tree(int, int *);
void run_tree(node *, int *);
void goodbye_tree(node *);

node *make_node(int fire) {
    node *a = (node *)malloc(sizeof(node));
    a->num = fire;
    a->mem = 0;
    a->dis = NULL;
    a->next = NULL;
    a->looked = false;
    return a;
}

void add_next(node *a1, node *a2, int distan) {
    a1->mem++;
    a2->mem++;
    int *buf1 = a1->dis;
    node **buf2 = a1->next;
    int *buf3 = a2->dis;
    node **buf4 = a2->next;
    a1->dis = (int *)malloc(sizeof(int)*a1->mem);
    a1->next = (node **)malloc(sizeof(node *)*a1->mem);
    a2->dis = (int *)malloc(sizeof(int)*a2->mem);
    a2->next = (node **)malloc(sizeof(node *)*a2->mem);
    for (int i=0; i<a1->mem-1; i++) {
        a1->dis[i] = buf1[i];
        a1->next[i] = buf2[i];
    }
    for (int i=0; i<a2->mem-1; i++) {
        a2->dis[i] = buf3[i];
        a2->next[i] = buf4[i];
    }
    a1->dis[a1->mem-1] = distan;
    a1->next[a1->mem-1] = a2;
    a2->dis[a2->mem-1] = distan;
    a2->next[a2->mem-1] = a1;
    free(buf1);
    free(buf2);
    free(buf3);
    free(buf4);
}

node *build_tree(int n, int *ans) {
    node *a[100000];
    for (int i=0; i<n; i++) {
        a[i] = make_node(i);
    }
    a[0]->looked = true;
    int u = 0, v = 0;
    long int w = 0;
    for (int i=0; i<n-1; i++) {
        scanf("%d %d %ld", &u, &v, &w);
        add_next(a[u-1], a[v-1], (int)w%2);
        if (a[u-1]->looked) run_tree(a[u-1], ans);
        else if (a[v-1]->looked) run_tree(a[v-1], ans);
    }
    return a[0];
}

void run_tree(node *start, int *ans) {
    start->looked = true;
    for (int i=0; i<start->mem; i++) {
        if (start->next[i]->looked) continue;
        if (start->dis[i] == 0) {
            ans[start->next[i]->num] = ans[start->num];
        }
        else {
            ans[start->next[i]->num] = 1 - ans[start->num];
        }
        run_tree(start->next[i], ans);
    }
}

void goodbye_tree(node *start) {
    start->looked = false;
    for (int i=0; i<start->mem; i++) {
        if (!start->next[i]->looked) continue;
        goodbye_tree(start->next[i]);
    }
    free(start->dis);
    free(start->next);
    free(start);
}

int main() {
    int n = 0;
    scanf("%d\n", &n);
    if (n > 50000) return 0;
    int ans[100000];
    ans[0] = 0;
    node *start = build_tree(n, ans);
    for (int i=0; i<n; i++) {
        printf("%d\n", ans[i]);
    }
    goodbye_tree(start);
    return 0;
}