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.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace A
{
    class StartingPoint
    {
        static void Main(string[] args)
        {
            if (args?.Any() == true)
            {
                var assembly = Assembly.GetEntryAssembly();
                var resourceName = "AtCoderCSharp.In.txt";
                var stream = assembly.GetManifestResourceStream(resourceName);
                var streamReader = new StreamReader(stream, Encoding.UTF8);
                var str = streamReader.ReadToEnd();
                var textReader = new StringReader(str);
                Console.SetIn(textReader);
            }

            try
            {
                foreach (var s in new Program().Solve())
                {
                    Console.WriteLine(s);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                if (args?.Any() != true)
                    throw e;
            }

            if (args?.Any() == true)
                Console.ReadKey();
        }
    }

    class Program
    {
        public List<int> ScanInts(int count) => Enumerable.Range(0, count).Select(_ => ScanInt).ToList();
        public List<int> ScanInts(long count) => ScanInts((int)count);
        public List<long> ScanLongs(int count) => Enumerable.Range(0, count).Select(_ => Scan).ToList();
        public List<long> ScanLongs(long count) => ScanLongs((int)count);
        public int ScanInt => int.Parse(Str);
        public long Scan => long.Parse(Str);
        public double ScanDouble => (double)Scan;

        StringBuilder sb = new StringBuilder();

        public string Str => ScanStr();
        public string ScanStr()
        {
            sb.Clear();

            int r = 0;
            bool? b = null;
            do
            {
                r = Console.Read();
                b = IsBreak(r);
            } while (b == true);

            if (b == null)
                throw new Exception("input error: unexpected end of stream");

            do
            {
                sb.Append((char)r);
                r = Console.Read();
                b = IsBreak(r);
            } while (b == false);

            return sb.ToString();
        }

        bool? IsBreak(int c)
        {
            switch (c)
            {
                case -1:
                    return null;
                case (int)' ':
                case (int)'\n':
                case (int)'\r':
                    return true;
                default:
                    return false;
            }
        }

        public IEnumerable<int> Loop(int count) => Enumerable.Range(0, count);
        public IEnumerable<int> Loop(int from, int to) => Enumerable.Range(from, to - from + 1);

        public IEnumerable<long> Loop(long count) => Loop(0, count - 1);
        public IEnumerable<long> Loop(long from, long to)
        {
            for (long i = from; i <= to; i++)
                yield return i;
        }

        public string YesNo(bool isYes) => isYes ? "Yes" : "No";

        public HashSet<T> ToHashSet<T>(IEnumerable<T> ts) => new HashSet<T>(ts);



        public IEnumerable<string> Solve()
        {
            var n = ScanInt;
            var tree = new Dictionary<int, List<int>>();
            //n = 200000;


            foreach (int i in Loop(n - 1))
            {
                var a = ScanInt;
                var b = ScanInt;

                //var a = i + 1;
                //var b = i + 2;

                if (a > b)
                {
                    var temp = b;
                    b = a;
                    a = temp;
                }

                if (tree.ContainsKey(a) == false)
                    tree.Add(a, new List<int>());

                tree[a].Add(b);
            }

            var colors = new Dictionary<int, int>();
            var checking = new List<int>();
            checking.Add(1);
            var color = 0;
            var c0 = 0;
            var c1 = 0;

            while (checking.Any())
            {
                foreach (var x in checking)
                    colors.Add(x, color);

                var newChecking = checking
                    .Where(x => tree.ContainsKey(x))
                    .SelectMany(x => tree[x])
                    .ToList();

                if (color == 0)
                    c0 += checking.Count;
                else
                    c1 += checking.Count;

                checking = newChecking;
                color = 1 - color;
            }

            //var c0 = colors.Values.Count(x => x == 0);
            //var c1 = colors.Values.Count(x => x == 1);

            var res = new List<int>();

            if (c0 <= n / 3)
            {
                var d0 = 3;
                var d1 = 1;
                var d2 = 2;


                foreach (int i in Loop(n))
                {
                    var ii = i + 1;
                    if (colors[ii] == 0)
                    {
                        res.Add(d0);
                        d0 += 3;
                    }
                    else
                    {
                        if (d1 <= n)
                        {
                            res.Add(d1);
                            d1 += 3;
                        }
                        else if (d2 <= n)
                        {
                            res.Add(d2);
                            d2 += 3;
                        }
                        else
                        {
                            res.Add(d0);
                            d0 += 3;
                        }
                    }

                }
            }
            else if (c1 <= n / 3)
            {
                var d0 = 3;
                var d1 = 1;
                var d2 = 2;


                foreach (int i in Loop(n))
                {
                    var ii = i + 1;
                    if (colors[ii] == 1)
                    {
                        res.Add(d0);
                        d0 += 3;
                    }
                    else
                    {
                        if (d1 <= n)
                        {
                            res.Add(d1);
                            d1 += 3;
                        }
                        else if (d2 <= n)
                        {
                            res.Add(d2);
                            d2 += 3;
                        }
                        else
                        {
                            res.Add(d0);
                            d0 += 3;
                        }
                    }

                }
            }
            else
            {
                var d0 = 3;
                var d1 = 1;
                var d2 = 2;


                foreach (int i in Loop(n))
                {
                    var ii = i + 1;
                    if (colors[ii] == 0)
                    {
                        if (d1 <= n)
                        {
                            res.Add(d1);
                            d1 += 3;
                        }
                        else
                        {
                            res.Add(d0);
                            d0 += 3;
                        }
                    }
                    else
                    {
                        if (d2 <= n)
                        {
                            res.Add(d2);
                            d2 += 3;
                        }
                        else
                        {
                            res.Add(d0);
                            d0 += 3;
                        }
                    }

                }
            }

            var sb = new StringBuilder();
            foreach (int x in res)
            {
                sb.Append(x);
                sb.Append(" ");
            }


            yield return sb.ToString();
        }
    }
}