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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static System.Math;

namespace Sample
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using (var sc = new SetConsole()) { Solve(); }
        }

        public static void Solve()
        {
            int n = GetString().ToInt();
            // 親、子、距離
            var tree = new Dictionary<int, Dictionary<int, int>>();
                        
            for (int i = 0; i < n - 1; i++)
            {
                var input = GetArray<int>();
                if (tree.ContainsKey(input[0]))
                {
                    tree[input[0]].Add(input[1], input[2]);
                }
                else
                {
                    tree.Add(input[0], new Dictionary<int, int>());
                    tree[input[0]].Add(input[1], input[2]);
                }
                if (tree.ContainsKey(input[1]))
                {
                    tree[input[1]].Add(input[0], input[2]);
                }
                else
                {
                    tree.Add(input[1], new Dictionary<int, int>());
                    tree[input[1]].Add(input[0], input[2]);
                }
            }
            BFS(tree);
        }

        public static void BFS( Dictionary<int, Dictionary<int, int>> tree)
        {
            var colors = new Dictionary<int, int>();
            
            var que = new Queue<int[]>();
            var startNodeIndex = tree.Keys.Min();
            colors.Add(startNodeIndex, 0);
            // index, 根からの距離
            que.Enqueue(new int[] { startNodeIndex, 0 });
            while (que.Any())
            {
                var tmp = que.Dequeue();
                var keys = tree[tmp[0]].Keys.ToArray();
                foreach (var key in keys)
                {
                    if (colors.ContainsKey(key)) { continue; }
                    tree[tmp[0]][key] += tmp[1];
                    tree[tmp[0]][key] %= 2;

                    if (tree[tmp[0]][key] == 0)
                    {
                        colors.Add(key, 0);
                    }
                    else
                    {
                        colors.Add(key, 1);
                    }
                    que.Enqueue(new int[] { key, tree[tmp[0]][key] });
                }
            }
            foreach (var key in colors.Keys.OrderBy(o => o))
            {
                CWrite(colors[key]);
            }
        }

        static void CWrite<T>(T str) => Console.WriteLine(str);
        static string GetString() => Console.ReadLine();
        static T[] GetArray<T>()
        {
            var str = Console.ReadLine();
            if (str == null) { throw new ArgumentNullException("標準入力がnullです 解答がおかしいはずです"); }

            var t = typeof(T);
            if (t == typeof(string))
            {
                return (T[])(object)str.Split();
            }
            if (t == typeof(int))
            {
                return (T[])(object)str.ToNumArray<T>();
            }
            if (t == typeof(long))
            {
                return (T[])(object)str.ToNumArray<T>();
            }
            if (t == typeof(double))
            {
                return (T[])(object)str.ToNumArray<T>();
            }
            throw new NotSupportedException($"{t} is not supported.");
        }
    }



    class SetConsole : IDisposable
    {
        readonly StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
        public SetConsole()
        {
            sw.AutoFlush = false;
            Console.SetOut(sw);
        }
        public void Dispose()
        {
            Console.Out.Flush();
            sw.AutoFlush = true;
            Console.SetOut(sw);
        }
    }

    static class ExtentionsLibrary
    {
        public static T[] CopyArray<T>(this T[] array)
        {
            var newArray = new T[array.Length];
            Array.Copy(array, newArray, array.Length);
            return newArray;
        }
        public static T[,] CopyArray<T>(this T[,] array)
        {
            var firstDimentionLength = array.GetLength(0);
            var secondDimentionLength = array.GetLength(1);
            var newDArray = new T[firstDimentionLength, secondDimentionLength];
            Array.Copy(array, newDArray, firstDimentionLength * secondDimentionLength);
            return newDArray;
        }
        public static T[] ToNumArray<T>(this string str)
        {
            var t = typeof(T);
            if (t == typeof(int))
            {
                return (T[])(object)str.Split().Select(int.Parse).ToArray();
            }
            if (t == typeof(long))
            {
                return (T[])(object)str.Split().Select(long.Parse).ToArray();
            }
            if (t == typeof(double))
            {
                return (T[])(object)str.Split().Select(double.Parse).ToArray();
            }
            throw new NotSupportedException();
        }
        public static int ToInt(this string str) => int.Parse(str);
        public static int ToInt(this char chr) => int.Parse(chr.ToString());
        public static long ToLong(this string str) => long.Parse(str);
        public static DateTime ToDateTime(this string str) => DateTime.Parse(str);
    }
}