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 Hitachi2020.Questions;
using Hitachi2020.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Hitachi2020.Questions
{
    public class QuestionC : AtCoderQuestionBase
    {
        List<int>[] _graph;
        bool[] _colors;

        public override IEnumerable<object> Solve(TextReader inputStream)
        {
            var n = inputStream.ReadInt();
            _graph = Enumerable.Range(0, n).Select(_ => new List<int>()).ToArray();
            _colors = new bool[n];

            for (int i = 0; i < n - 1; i++)
            {
                var ab = inputStream.ReadIntArray();
                var a = ab[0] - 1;
                var b = ab[1] - 1;
                _graph[a].Add(b);
                _graph[b].Add(a);
            }

            Paint(0, -1, true);

            var reds = Enumerable.Range(0, n).Where(i => _colors[i]).ToArray();
            var blues = Enumerable.Range(0, n).Where(i => !_colors[i]).ToArray();
            var result = new int[n];

            if (reds.Length <= n / 3 || blues.Length <= n / 3)
            {
                var many = reds.Length <= n / 3 ? blues : reds;
                var few = reds.Length <= n / 3 ? reds : blues;

                // mod 0埋め
                var cursor = 0;
                for (int i = 1; i * 3 <= n; i++)
                {
                    if (cursor >= few.Length)
                    {
                        break;
                    }
                    result[few[cursor++]] = i * 3;
                }

                cursor = 0;
                for (int i = result[few[few.Length - 1]] + 1; i * 3 <= n; i++)
                {
                    result[many[cursor++]] = i * 3;
                }

                for (int i = 0; i * 3 + 1 <= n; i++)
                {
                    result[many[cursor++]] = i * 3 + 1;
                }

                for (int i = 0; i * 3 + 2 <= n; i++)
                {
                    result[many[cursor++]] = i * 3 + 2;
                }
            }
            else
            {
                var redCursor = 0;
                for (int i = 0; i * 3 + 1 <= n; i++)
                {
                    result[reds[redCursor++]] = i * 3 + 1;
                }

                var blueCursor = 0;
                for (int i = 0; i * 3 + 2 <= n; i++)
                {
                    result[blues[blueCursor++]] = i * 3 + 2;
                }

                for (int i = 1; i * 3 <= n; i++)
                {
                    if (redCursor < reds.Length)
                    {
                        result[reds[redCursor++]] = i * 3;
                    }
                    else
                    {
                        result[blues[blueCursor++]] = i * 3;
                    }
                }
            }

            yield return string.Join(" ", result);
        }

        void Paint(int current, int prev, bool color)
        {
            _colors[current] = color;

            foreach (var next in _graph[current])
            {
                if (next == prev)
                {
                    continue;
                }

                Paint(next, current, !color);
            }
        }
    }
}


namespace Hitachi2020
{
    class Program
    {
        static void Main(string[] args)
        {
            IAtCoderQuestion question = new QuestionC();    // 問題に合わせて書き換え
            var answers = question.Solve(Console.In);
            foreach (var answer in answers)
            {
                Console.WriteLine(answer);
            }
        }
    }
}

#region Base Classes

namespace Hitachi2020.Questions
{

    public interface IAtCoderQuestion
    {
        IEnumerable<object> Solve(string input);
        IEnumerable<object> Solve(TextReader inputStream);
    }

    public abstract class AtCoderQuestionBase : IAtCoderQuestion
    {
        public IEnumerable<object> Solve(string input)
        {
            var stream = new MemoryStream(Encoding.Unicode.GetBytes(input));
            var reader = new StreamReader(stream, Encoding.Unicode);

            return Solve(reader);
        }

        public abstract IEnumerable<object> Solve(TextReader inputStream);
    }

}

#endregion

#region Extensions

namespace Hitachi2020.Extensions
{
    internal static class TextReaderExtensions
    {
        internal static int ReadInt(this TextReader reader) => int.Parse(ReadString(reader));
        internal static long ReadLong(this TextReader reader) => long.Parse(ReadString(reader));
        internal static double ReadDouble(this TextReader reader) => double.Parse(ReadString(reader));
        internal static string ReadString(this TextReader reader) => reader.ReadLine();

        internal static int[] ReadIntArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(int.Parse).ToArray();
        internal static long[] ReadLongArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(long.Parse).ToArray();
        internal static double[] ReadDoubleArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(double.Parse).ToArray();
        internal static string[] ReadStringArray(this TextReader reader, char separator = ' ') => reader.ReadLine().Split(separator);
    }
}

#endregion