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 Rust (1.15.1) (judge verdict: Runtime Error)

use std::collections::VecDeque;
use std::io::stdin;

fn coloring(matrix: Vec<Vec<Option<u64>>>) -> Vec<Option<bool>> {
    assert_eq!(matrix.len(), matrix[0].len());

    let num_node = matrix.len();

    let mut color_allocation: Vec<Option<bool>> = vec![None; num_node];

    while let Some(root_index) = (0..num_node).find(|&i| color_allocation[i].is_none()) {
        let mut index_queue: VecDeque<u32> = VecDeque::new();
        color_allocation[root_index] = Some(true);
        index_queue.push_back(root_index as u32);

        while let Some(index) = index_queue.pop_front() {
            let indices: Vec<usize> = color_allocation
                .iter()
                .enumerate()
                .filter(|&(_, c)| c.is_none())
                .map(|(i, _)| i)
                .collect();

            for i in indices.into_iter() {
                if let Some(distance) = matrix[index as usize][i] {
                    index_queue.push_back(i as u32);
                    color_allocation[i] = color_allocation[index as usize].map(|c| {
                        let b = distance % 2 != 0;
                        (c && !b) | (!c && b)
                    })
                }
            }
        }
    }
    return color_allocation;
}

fn main() {
    let mut input = String::new();
    let _ = stdin().read_line(&mut input).unwrap();
    let num_node = input.trim().parse().unwrap();

    let mut graph: Vec<Vec<Option<u64>>> = vec![vec![None; num_node]; num_node];

    for (u, v, w) in (1..num_node).map(|_| {
        let mut input = String::new();
        let _ = stdin().read_line(&mut input).unwrap();
        let input: Vec<String> = input.trim().split(" ").map(|s| s.to_string()).collect();
        assert_eq!(3, input.len());
        (
            input[0].parse::<u32>().unwrap(),
            input[1].parse::<u32>().unwrap(),
            input[2].parse::<u64>().unwrap(),
        )
    }) {
        let (i, j) = ((u - 1) as usize, (v - 1) as usize);
        graph[i][j] = Some(w);
        graph[j][i] = Some(w);
    }

    let color_allocation = coloring(graph);

    for c in color_allocation.into_iter() {
        println!("{}", c.map(|c| if c { "1" } else { "0" }).unwrap())
    }
}