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

using System;
using System.Collections.Generic;
using CompLib.Util;

public class Program
{
    private int N;
    private List<int>[] Edges;

    // 3で割ったあまり
    private int[] M;

    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        Edges = new List<int>[N];
        for (int i = 0; i < N; i++)
        {
            Edges[i] = new List<int>();
        }

        for (int i = 0; i < N - 1; i++)
        {
            int a = sc.NextInt() -1;
            int b = sc.NextInt() -1;
            Edges[a].Add(b);
            Edges[b].Add(a);
        }

        M = new int[3];
        M[0] = N / 3;
        M[1] = (N + 2) / 3;
        M[2] = (N + 1) / 3;

        var ans = new int[N];
        var q = new Queue<int>();
        for (int i = 0; i < N; i++)
        {
            ans[i] = -1;
        }

        ans[0] = 1;
        M[1]--;
        q.Enqueue(0);
        while (q.Count > 0)
        {
            var p = q.Dequeue();
            foreach (int i in Edges[p])
            {
                if (ans[i] != -1) continue;
                if (ans[p] == 1)
                {
                    if (M[2] > 0)
                    {
                        ans[i] = 2;
                        M[2]--;
                    }
                    else
                    {
                        ans[i] = 0;
                        M[0]--;
                    }

                    
                }
                else if (ans[p] == 2)
                {
                    if (M[1] > 0)
                    {
                        ans[i] = 1;
                        M[1]--;
                    }
                    else
                    {
                        ans[i] = 0;
                        M[0]--;
                    }
                }
                else if (ans[p] == 0)
                {
                    if (M[0] > 0)
                    {
                        ans[i] = 0;
                        M[0]--;
                    }
                }
                q.Enqueue(i);
            }
        }
        
        var anser = new int[N];

        int z = 3;
        int o = 1;
        int t = 2;
        for (int i = 0; i < N; i++)
        {
            if (ans[i] == 0)
            {
                anser[i] = z;
                z += 3;
            }
            else if (ans[i] == 1)
            {
                anser[i] = o;
                o += 3;
            }
            else if (ans[i] == 2)
            {
                anser[i] = t;
                t += 3;
            }
        }
        
        Console.WriteLine(string.Join(" ",anser));
    }
    

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}