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

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<Dictionary<int, int>>();
            var distanseQue = new Queue<int>();
            var startNodeIndex = tree.Keys.Min();
            colors.Add(startNodeIndex, 0);
            que.Enqueue(tree[startNodeIndex]);
            distanseQue.Enqueue(0);
            while (que.Any())
            {
                var tmp = que.Dequeue();
                var distanse = distanseQue.Dequeue();
                var keys = tmp.Keys.ToArray();
                foreach (var key in keys)
                {
                    if (colors.ContainsKey(key)) { continue; }
                    tmp[key] += distanse;

                    if (tmp[key] % 2 == 0)
                    {
                        colors.Add(key, 0);
                    }
                    else
                    {
                        colors.Add(key, 1);
                    }
                    que.Enqueue(tree[key]);
                    distanseQue.Enqueue(tmp[key] % 2);
                }
            }
            foreach (var value in colors.Values)
            {
                CWrite(value);
            }
        }

        static void CWrite<T>(T str) => Console.WriteLine(str);
        static string GetString() => Console.ReadLine();
        static T[] GetArray<T>()
        {
            var t = typeof(T);
            var str = Console.ReadLine();

            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);
    }
}