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;
    bool connect;
    int dis_pre;
    struct Node *pre;
    int mem;
    int *dis;
    struct Node **next;
} node;

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

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

void del_next(node *parent, node *child) {
    int check;
    for (int i=0; i<parent->mem; i++) {
        if (parent->next[i] == child) {
            check = i;
            break;
        }
    }
    parent->mem--;
    int *buf1 = parent->dis;
    node **buf2 = parent->next;
    parent->dis = (int *)malloc(sizeof(int)*parent->mem);
    parent->next = (node **)malloc(sizeof(node *)*parent->mem);
    for (int i=0; i<parent->mem+1; i++) {
        if (i < check) {
            parent->dis[i] = buf1[i];
            parent->next[i] = buf2[i];
        }
        else if (i > check) {
            parent->dis[i-1] = buf1[i];
            parent->next[i-1] = buf2[i];
        }
    }
    free(buf1);
    free(buf2);
}

void add_next(node *parent, node *child, int distan) {
    if (child->pre != NULL) {
        del_next(child->pre, child);
        add_next(child, child->pre, child->dis_pre);
    }
    child->pre = parent;
    child->dis_pre = distan;
    parent->mem++;
    int *buf1 = parent->dis;
    node **buf2 = parent->next;
    parent->dis = (int *)malloc(sizeof(int)*parent->mem);
    parent->next = (node **)malloc(sizeof(node *)*parent->mem);
    for (int i=0; i<parent->mem-1; i++) {
        parent->dis[i] = buf1[i];
        parent->next[i] = buf2[i];
    }
    parent->dis[parent->mem-1] = distan;
    parent->next[parent->mem-1] = child;
    free(buf1);
    free(buf2);
}

void you_are_true(node *a, int *ans) {
    a->connect = true;
    if (a->dis_pre == 0) {
        ans[a->num] = ans[a->pre->num];
    }
    else {
        ans[a->num] = 1 - ans[a->pre->num];
    }
    if (a->mem == 0) return;
    for (int i=0; i<a->mem; i++) {
        you_are_true(a->next[i], ans);
    }
}

void making(node **a, int u, int v, int w, int *ans) {
    if ((!a[u]->connect) && (!a[v]->connect)) {
        add_next(a[v], a[u], w);
    }
    else if (a[u]->connect) {
        add_next(a[u], a[v], w);
        you_are_true(a[v], ans);
    }
    else if (a[v]->connect) {
        add_next(a[v], a[u], w);
        you_are_true(a[u], ans);
    }
}

node *build_tree(int n, int *ans) {
    node *a[n];
    for (int i=0; i<n; i++) {
        a[i] = make_node(i);
    }
    a[0]->connect = 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);
        making(a, u-1, v-1, (int)w%2, ans);
    }
    return a[0];
}

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

int main() {
    int n = 0;
    scanf("%d\n", &n);
    int ans[n];
    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;
}