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: Time Limit Exceeded)

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

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

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

node *make_node(int fire) {
    node *a = (node *)malloc(sizeof(node));
    a->num = fire;
    a->mem = 0;
    a->dis = NULL;
    a->next = NULL;
    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) {
    node *a[100000];
    for (int i=0; i<n; i++) {
        a[i] = make_node(i);
    }
    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);
    }
    return a[0];
}

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

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

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