Problem:
Score : 600 points

Problem Statement

We have a tree with N vertices. The vertices are numbered 1 to N, and the i-th edge connects Vertex a_i and Vertex b_i.

Takahashi loves the number 3. He is seeking a permutation p_1, p_2, \ldots , p_N of integers from 1 to N satisfying the following condition:

For every pair of vertices (i, j), if the distance between Vertex i and Vertex j is 3, the sum or product of p_i and p_j is a multiple of 3.

Here the distance between Vertex i and Vertex j is the number of edges contained in the shortest path from Vertex i to Vertex j.

Help Takahashi by finding a permutation that satisfies the condition.

Constraints

2\leq N\leq 2\times 10^5

1\leq a_i, b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}

Output

If no permutation satisfies the condition, print -1.

Otherwise, print a permutation satisfying the condition, with space in between.
If there are multiple solutions, you can print any of them.

Sample Input 1

5
1 2
1 3
3 4
3 5

Sample Output 1

1 2 5 4 3

The distance between two vertices is 3 for the two pairs (2, 4) and (2, 5).

p_2 + p_4 = 6

p_2\times p_5 = 6

Thus, this permutation satisfies the condition.

Solution in C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;
using System.Linq;

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            input_iter = LazyLoadAllLines();
            //------------------------------------------------//
            //var n = Read<long>();
            //var s = Read();
            //var als = Read<long>(n);
            //var sls = Read(n);
            /*List<Tuple<string, long>> scls = new List<Tuple<string, long>>();
            for (int i = 0; i < m; ++i)
            {
                scls.Add(Tuple.Create(Read(), Read<long>()));
            }*/
            //List<List<long>> abls = new List<List<long>>();
            //List<long> cls = new List<long>();
            /*List<long> edgeNum = Enumerable.Repeat(0L, (int)n).ToList();
            for (int i = 0; i < n - 1; ++i)
            {
                abls.Add(Read<long>(2));
                abls[i].Add(i);
                abls[i].Add(-1);
                --abls[i][0];
                --abls[i][1];
                //cls.Add(Read<long>());
                ++edgeNum[(int)abls[i][0]];
                ++edgeNum[(int)abls[i][1]];
            }*/

            //als.Sort((l, r) => (r - l));
            //sls.Sort(StringComparer.OrdinalIgnoreCase);

            var n = Read<int>();
            List<int>[] edgedNodes = new List<int>[n + 1];
            for (int i = 1; i <= n; i++) edgedNodes[i] = new List<int>();
            for (int i = 0; i < n - 1; i++)
            {
                var a = Read<int>();
                var b = Read<int>();
                edgedNodes[a].Add(b);
                edgedNodes[b].Add(a);
            }
            //var abls = Read<int>(n - 1, 2, true);

            int[] depths = new int[n + 1];
            depths[1] = 0;

            List<int> depth1 = new List<int>();

            List<int>[] ancesters = new List<int>[n + 1];
            for (int i = 1; i <= n; i++) ancesters[i] = new List<int>();
            // parents[1] = new HashSet<int>();

            Queue<int> lookingNodes = new Queue<int>();
            lookingNodes.Enqueue(1);

            while (lookingNodes.Count() > 0)
            {
                var node = lookingNodes.Dequeue();

                var linkedNodeNum = edgedNodes[node].Count();
                if (linkedNodeNum > 1)
                {
                    // has child
                    for (int i = 0; i < linkedNodeNum; i++)
                    {
                        var child = edgedNodes[node][i];
                        if (ancesters[node].Contains(child)) continue;

                        depths[child] = depths[node] + 1;
                        if (depths[child] == 1) depth1.Add(child);
                        ancesters[child] = new List<int>(ancesters[node]);
                        ancesters[child].Add(node);

                        lookingNodes.Enqueue(child);
                    }
                }
                // else: leaf
            }

            HashSet<Tuple<int, int>> dist3s = new HashSet<Tuple<int, int>>();
            for (int i = 1; i <= n; ++i)
            {
                if(depths[i] == 2)
                {
                    for (int j = 0; j < depth1.Count(); j++)
                    {
                        if(depth1[j] != ancesters[i][1])
                        {
                            dist3s.Add(Tuple.Create(i, depth1[j]));
                        }
                    }
                }
                else if(depths[i] > 2)
                {
                    dist3s.Add(Tuple.Create(ancesters[i][depths[i] - 3], i));
                }
            }

            List<int> numbers = Enumerable.Range(0, n + 1).ToList();
            List<bool> isLocked = new List<bool>(Enumerable.Repeat(false, n + 1));
            int next3 = 3;

            foreach (var pair in dist3s)
            {
                if((numbers[pair.Item1] + numbers[pair.Item2]) % 3 == 0)
                {
                    isLocked[pair.Item1] = true;
                    isLocked[pair.Item2] = true;
                }
                else if(numbers[pair.Item1] % 3 != 0 && numbers[pair.Item2] % 3 != 0)
                {
                    if (!isLocked[pair.Item1])
                    {
                        isLocked[pair.Item1] = true;
                        var temp = numbers[pair.Item1];
                        numbers[pair.Item1] = numbers[next3];
                        numbers[next3] = temp;

                        do
                        {
                            next3 += 3;
                        } while (next3 <= n && isLocked[next3]);
                    }else if (!isLocked[pair.Item2])
                    {
                        isLocked[pair.Item2] = true;
                        var temp = numbers[pair.Item2];
                        numbers[pair.Item2] = numbers[next3];
                        numbers[next3] = temp;

                        do
                        {
                            next3 += 3;
                        } while (next3 <= n && isLocked[next3]);
                    }
                }
            }

            numbers.RemoveAt(0);
            Console.WriteLine(string.Join(" ", numbers));
        }

        static IEnumerator<string> input_iter;

        static IEnumerator<string> LazyLoadAllLines(bool doSplit = true)
        {
            string line;
            if (doSplit)
            {
                do
                {
                    line = Console.ReadLine();
                    foreach (var s in line.Split(' '))
                        yield return s;
                } while (line != null);
            }
            else
            {
                do
                {
                    line = Console.ReadLine();
                    yield return line;
                } while (line != null);
            }
        }
        // single 1 elem.
        static _T Read<_T>() where _T : struct
        {
            input_iter.MoveNext();
            return (_T)Convert.ChangeType(input_iter.Current, typeof(_T));
        }
        static string Read()
        {
            input_iter.MoveNext();
            return input_iter.Current;
        }
        // single n-elems.
        static List<_T> Read<_T>(long num) where _T : struct
        {
            List<_T> res = new List<_T>();
            for (long i = 0; i < num; ++i)
            {
                input_iter.MoveNext();
                res.Add((_T)Convert.ChangeType(input_iter.Current, typeof(_T)));
            }
            return res;
        }
        static List<string> Read(long num)
        {
            List<string> res = new List<string>();
            for (long i = 0; i < num; ++i)
            {
                input_iter.MoveNext();
                res.Add(input_iter.Current);
            }
            return res;
        }
        // multiple(single type) n-elems. (Range, Edge etc.)
        static List<List<_T>> Read<_T>(long elemNum, long typeNum, bool toSort = false) where _T : struct
        {
            List<List<_T>> res = new List<List<_T>>();
            for (int i = 0; i < elemNum; ++i)
            {
                var elem = Read<_T>(typeNum);
                if (toSort) elem.Sort();
                res.Add(elem);
            }
            return res;
        }
        // multiple(multiple type) n-elems.
        static List<Tuple<_T1, _T2>> Read<_T1, _T2>(long num) where _T1 : struct where _T2 : struct
        {
            List<Tuple<_T1, _T2>> res = new List<Tuple<_T1, _T2>>();
            for (int i = 0; i < num; ++i)
            {
                res.Add(Tuple.Create(Read<_T1>(), Read<_T2>()));
            }
            return res;
        }
    }
}