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: Accepted)

use std::io::*;
use std::str::FromStr;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}

fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
    let mut i = CpReader::new(i);
    let n = i.read::<usize>();

    let mut edges = VecVec32::new(n, (n - 1) * 2);
    for _ in 0..n - 1 {
        let (u, v, w) = i.read3::<usize, usize, usize>();
        let (u, v, w) = (u - 1, v - 1, (w % 2) == 1);
        edges.push(u, U31Bool::new(v as u32, w));
        edges.push(v, U31Bool::new(u as u32, w));
    }
    let mut ds = vec![false; n];
    let mut q = vec![(0u32, 0u32)];
    while let Some((i0, i1)) = q.pop() {
        let d = ds[i1 as usize];
        for &i2_w in edges.get(i1 as usize) {
            let i2 = i2_w.u31_value();
            let w = i2_w.bool_value();
            if i2 != i0 {
                ds[i2 as usize] = d ^ w;
                q.push((i1, i2));
            }
        }
    }
    for d in ds {
        writeln!(o, "{}", if d { 1 } else { 0 }).unwrap();
    }
}
#[derive(Copy, Clone)]
struct U31Bool(u32);
impl U31Bool {
    fn new(u31_value: u32, bool_value: bool) -> U31Bool {
        U31Bool((u31_value << 1) | if bool_value { 1 } else { 0 })
    }
    fn u31_value(&self) -> u32 {
        self.0 >> 1
    }
    fn bool_value(&self) -> bool {
        (self.0 & 1) == 1
    }
}

mod utils {
    use super::*;

    pub struct CpReader<R: BufRead> {
        r: R,
        s: String,
    }

    macro_rules! fn_read {
        { $f:ident($($t:ident),*) } => {
            #[allow(dead_code)]
            pub fn $f<$($t: FromStr),*>(&mut self) -> ($($t),*) {
                let i = &mut self.read_line().split(' ');
                ($(read_next::<$t,_>(i)),*)
            }
        };
    }

    impl<R: BufRead> CpReader<R> {
        pub fn new(r: R) -> Self {
            CpReader {
                r: r,
                s: String::new(),
            }
        }
        pub fn read_line(&mut self) -> &str {
            self.s.clear();
            self.r.read_line(&mut self.s).unwrap();
            self.s.trim()
        }

        pub fn read<T: FromStr>(&mut self) -> T {
            self.read_line().parse().ok().unwrap()
        }
        fn_read! { read2(T1, T2) }
        fn_read! { read3(T1, T2, T3) }
        fn_read! { read4(T1, T2, T3, T4) }
        fn_read! { read5(T1, T2, T3, T4, T5) }
    }
    fn read_next<'a, T: FromStr, I: Iterator<Item = &'a str>>(i: &mut I) -> T {
        i.next().unwrap().parse().ok().unwrap()
    }

    pub struct VecVec32<T> {
        hs: Vec<u32>,
        vs: Vec<(T, u32)>,
    }
    #[allow(dead_code)]
    impl<T> VecVec32<T> {
        #[inline]
        pub fn new(len: usize, capacity: usize) -> Self {
            assert!(capacity < u32::max_value() as usize);
            VecVec32 {
                hs: vec![0; len],
                vs: Vec::with_capacity(capacity),
            }
        }

        #[inline]
        pub fn push(&mut self, idx: usize, value: T) {
            self.vs.push((value, self.hs[idx]));
            self.hs[idx] = self.vs.len() as u32;
        }

        #[inline]
        pub fn len(&self) -> usize {
            self.hs.len()
        }

        #[inline]
        pub fn get(&self, idx: usize) -> VecVec32Iter<T> {
            VecVec32Iter {
                s: self,
                i: self.hs[idx],
            }
        }
    }
    pub struct VecVec32Iter<'a, T: 'a> {
        s: &'a VecVec32<T>,
        i: u32,
    }
    impl<'a, T: 'a> Iterator for VecVec32Iter<'a, T> {
        type Item = &'a T;

        #[inline]
        fn next(&mut self) -> Option<Self::Item> {
            if self.i == 0 {
                None
            } else {
                let i = self.i as usize - 1;
                self.i = self.s.vs[i].1;
                Some(&self.s.vs[i].0)
            }
        }
    }

}