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

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

namespace HITACHI2020C
{
    class Input
    {
        /// <summary>
        /// 1行の入力を取得する
        /// </summary>
        /// <returns>文字列</returns>
        public string String()
        {
            return Console.ReadLine();
        }

        /// <summary>
        /// 複数行の入力を取得
        /// </summary>
        /// <returns>文字列の配列</returns>
        public string[] ArrayString(int rowNumber)
        {
            string[] s = new string[rowNumber];
            for (int i = 0; i < rowNumber; i++)
            {
                s[i] = String();
            }

            return s;
        }

        /// <summary>
        /// 1行の入力を取得
        /// </summary>
        /// <returns>int型の値</returns>
        public int Int()
        {
            return int.Parse(String());
        }

        /// <summary>
        /// 1行の入力を取得
        /// </summary>
        /// <returns>int型の配列</returns>
        public int[] ArrayInt()
        {
            return Split().Select(int.Parse).ToArray();
        }

        /// <summary>
        /// 複数行の入力を取得
        /// </summary>
        /// <param name="rowNumber">行数</param>
        /// <returns>int型の配列</returns>
        public int[] ArrayInt(int rowNumber)
        {
            int[] ints = new int[rowNumber];
            for (int i = 0; i < rowNumber; i++)
            {
                ints[i] = Int();
            }

            return ints;
        }

        /// <summary>
        /// 1行の入力を取得
        /// </summary>
        /// <returns>long型の値</returns>
        public long Long()
        {
            return long.Parse(String());
        }

        /// <summary>
        /// 2つの整数が1行に書かれている入力を、2つのlongで受け取る
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        public void Longs(ref long A, ref long B)
        {
            var longs = ArrayLong();
            A = longs[0];
            B = longs[1];
        }

        /// <summary>
        /// 3つの整数が1行に書かれている入力を、3つのlongで受け取る
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        /// <param name="C"></param>
        public void Longs(ref long A, ref long B, ref long C)
        {
            var longs = ArrayLong();
            A = longs[0];
            B = longs[1];
            C = longs[2];
        }

        /// <summary>
        /// 4つの整数が1行に書かれている入力を、4つのlongで受け取る
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        /// <param name="C"></param>
        /// <param name="D"></param>
        public void Longs(ref long A, ref long B, ref long C, ref long D)
        {
            var longs = ArrayLong();
            A = longs[0];
            B = longs[1];
            C = longs[2];
            D = longs[3];
        }

        /// <summary>
        /// 2つの整数が複数行に書かれている入力を、2つのlong[]で受け取る
        /// </summary>
        /// <param name="rowNumber"></param>
        /// <param name="A"></param>
        /// <param name="B"></param>
        public void LongsArray(long rowNumber, ref long[] A, ref long[] B)
        {
            A = new long[rowNumber];
            B = new long[rowNumber];
            for (int i = 0; i < rowNumber; i++)
            {
                var l = ArrayLong();
                A[i] = l[0];
                B[i] = l[1];
            }
        }

        /// <summary>
        /// 3つの整数が複数行に書かれている入力を、2つのlong[]で受け取る
        /// </summary>
        /// <param name="rowNumber"></param>
        /// <param name="A"></param>
        /// <param name="B"></param>
        /// <param name="C"></param>
        public void LongsArray(long rowNumber, ref long[] A, ref long[] B, ref long[] C)
        {
            A = new long[rowNumber];
            B = new long[rowNumber];
            C = new long[rowNumber];
            for (int i = 0; i < rowNumber; i++)
            {
                var l = ArrayLong();
                A[i] = l[0];
                B[i] = l[1];
                C[i] = l[2];
            }
        }

        /// <summary>
        /// 1行の入力を取得
        /// </summary>
        /// <returns>long型の配列</returns>
        public long[] ArrayLong()
        {
            return Split().Select(long.Parse).ToArray();
        }

        public long[] ArrayLong(int rowNumber)
        {
            long[] longs = new long[rowNumber];
            for (int i = 0; i < rowNumber; i++)
            {
                longs[i] = Long();
            }

            return longs;
        }

        /// <summary>
        /// 1行の入力を取得
        /// </summary>
        /// <returns>double型の値</returns>
        public double Double()
        {
            return double.Parse(String());
        }

        /// <summary>
        /// 1行の入力を取得
        /// </summary>
        /// <returns>double型の配列</returns>
        public double[] ArrayDouble()
        {
            return Split().Select(double.Parse).ToArray();
        }

        private IEnumerable<string> Split()
        {
            return String().Split(' ');
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Solver solver = new Solver();
            solver.Solve();
        }
    }

    class Solver
    {
        public Solver()
        {
            Input input = new Input();
            N = input.Long();
            input.LongsArray(N - 1, ref a, ref b);
        }

        private long N;
        private long[] a;
        private long[] b;

        public void Solve()
        {
            HashSet<long>[] e = new HashSet<long>[N + 1];
            for (int i = 0; i <= N; i++)
            {
                e[i] = new HashSet<long>();
            }

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

            HashSet<long> yellow = new HashSet<long>();
            HashSet<long> red = new HashSet<long>();

            Queue<QQ> q = new Queue<QQ>();
            HashSet<long> already = new HashSet<long>();
            red.Add(1);
            already.Add(1);

            q.Enqueue(new QQ(1, true));
            while (q.Count > 0)
            {
                var v = q.Dequeue();
                foreach (var l in e[v.L])
                {
                    if (already.Contains(l))
                    {
                        continue;
                    }

                    q.Enqueue(new QQ(l, v.IsRed ^ true));
                    if (v.IsRed)
                    {
                        yellow.Add(l);
                    }
                    else
                    {
                        red.Add(l);
                    }

                    already.Add(l);
                }
            }

            Stack<long>[] modThree = new Stack<long>[3];
            for (int i = 0; i < 3; i++)
            {
                modThree[i] = new Stack<long>();
            }

            for (int i = 1; i <= N; i++)
            {
                modThree[i % 3].Push(i);
            }

            long[] ans = new long[N + 1];

            if (red.Count > N / 3 && yellow.Count > N / 3)
            {
                foreach (long r in red)
                {
                    if (modThree[1].Count > 0)
                    {
                        ans[r] = modThree[1].Pop();
                    }
                    else
                    {
                        ans[r] = modThree[0].Pop();
                    }
                }

                foreach (long y in yellow)
                {
                    if (modThree[2].Count > 0)
                    {
                        ans[y] = modThree[2].Pop();
                    }
                    else
                    {
                        ans[y] = modThree[0].Pop();
                    }
                }
            }
            else if (red.Count > N / 3)
            {
                foreach (long y in yellow)
                {
                    ans[y] = modThree[0].Pop();;
                }

                foreach (long r in red)
                {
                    if (modThree[0].Count > 0)
                    {
                        ans[r] = modThree[0].Pop();
                    }
                    else if (modThree[1].Count > 0)
                    {
                        ans[r] = modThree[1].Pop();
                    }
                    else
                    {
                        ans[r] = modThree[2].Pop();
                    }
                }
            }
            else
            {
                foreach (long r in red)
                {
                    ans[r] = modThree[0].Pop();;
                }

                foreach (long y in yellow)
                {
                    if (modThree[0].Count > 0)
                    {
                        ans[y] = modThree[0].Pop();
                    }
                    else if (modThree[1].Count > 0)
                    {
                        ans[y] = modThree[1].Pop();
                    }
                    else
                    {
                        ans[y] = modThree[2].Pop();
                    }
                }
            }

            for (int i = 1; i <= N; i++)
            {
                Console.Write(ans[i] + " ");
            }

            Console.WriteLine();
        }
    }

    class QQ
    {
        public long L;
        public bool IsRed;

        public QQ(long l, bool isRed)
        {
            L = l;
            IsRed = isRed;
        }
    }
}