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: Wrong Answer)

#[allow(dead_code)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(dead_code)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(dead_code)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input!{
        n: usize,
        mut edges: [(usize1, usize1, usize); n - 1]
    }
    let mut e2 = edges.iter().map(|&(u, v, w)| (v, u ,w)).collect();
    edges.append(&mut e2);
    edges.sort_by(ord);

    // kuro
    // println!("{:?}", edges);
    let mut tbl = vec![0; n];
    // println!("{:?}", tbl);
    // to white
    paint(&mut tbl, &edges, 0);
    // println!("{:?}", tbl);

    for v in tbl {
        println!("{}", v);
    }
}

fn ord(l: &(usize, usize, usize), r: &(usize, usize, usize)) -> std::cmp::Ordering {
    let lu = l.0;
    let ru = r.0;
    lu.cmp(&ru)
}

fn paint(table: &mut Vec<u8>, edges: &Vec<(usize, usize, usize)>, i: usize) {
    // println!("i: {}", i);
    table[i] = 1;
    let begin = lower_bound_by(edges, &(i, 0, 0), ord);
    let end = upper_bound_by(edges, &(i, 0, 0), ord);
    for k in begin..end {
        let (_current, next, weight) = edges[k];
        // println!("  edges[{}]: current: {}, next: {}, weight: {}", k, _current, next, weight);
        if weight % 2 == 0 && table[next] == 0 {
            paint(table, edges, next);
        }
    }
}

#[allow(dead_code)]
fn lower_bound_by<T, F>(v: &Vec<T>, x: &T, f: F) -> usize 
where
T: Ord,
F: Fn(&T, &T) -> std::cmp::Ordering {
    let mut left = 0;
    let mut right = v.len();
    while left < right {
        let mid = (left + right) / 2;
        let value = &v[mid];
        let cmp = f(x, value);
        if cmp == std::cmp::Ordering::Greater {
            left = mid + 1;
        } else {
            right = mid;
        }
    }
    return right;
}

#[allow(dead_code)]
fn lower_bound<T>(v: &Vec<T>, x: &T) -> usize 
where T: Ord {
    let mut left = 0;
    let mut right = v.len();
    while left < right {
        let mid = (left + right) / 2;
        let value = &v[mid];
        if x <= value {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    return right;
}

#[allow(dead_code)]
fn upper_bound_by<T, F>(v: &Vec<T>, x: &T, f: F) -> usize 
where
T: Ord,
F: Fn(&T, &T) -> std::cmp::Ordering {
    let mut left = 0;
    let mut right = v.len();
    while left < right {
        let mid = (left + right) / 2;
        let value = &v[mid];
        let cmp = f(x, value);
        if cmp == std::cmp::Ordering::Less {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    return left;
}

#[allow(dead_code)]
fn upper_bound<T>(v: &Vec<T>, x: &T) -> usize 
where T: Ord {
    let mut left = 0;
    let mut right = v.len();
    while left < right {
        let mid = (left + right) / 2;
        let value = &v[mid];
        if x < value {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    return left;
}