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;
class Program
{
	static void Main(string[] args)
	{
		int N = int.Parse(Console.ReadLine());
		int[,]A = new int[N-1,2];
		var list = new List<int>[N];
		int[]P = new int[N];
		var E = new List<int>[N];
		for(var i=0;i<N;i++){
			list[i] = new List<int>();
			E[i] = new List<int>();
		}
		for(var i=0;i<N-1;i++){
			string[] str = Console.ReadLine().Split();
			A[i,0] = int.Parse(str[0]);
			A[i,1] = int.Parse(str[1]);
			if(A[i,1] < A[i,0]){
				int t = A[i,1];
				A[i,1] = A[i,0];
				A[i,0] = t;
			}
			list[A[i,0]-1].Add(A[i,1]);
			P[A[i,1]-1]=A[i,0];
		}
		var list1 = new List<int>();
		var list2 = new List<int>();
		var lista = new List<int>();
		lista.Add(1);
		list2.Add(1);
		var lt = 0;
		while(lista.Count>0){
			lt++;
			var lista2 = new List<int>(lista);
			lista.Clear();
			foreach(var i in lista2){
				foreach(var j in list[i-1]){
					if(lt%2==1){
						list1.Add(j);
					} else {
						list2.Add(j);
					}
					lista.Add(j);
				}
			}
		}
		int a = 3;
		int f = 1;
		int[]ans = new int[N];
		if(list1.Count * 3 < N){
			for(var i=0;i<list1.Count;i++){
				ans[list1[i]-1] = a;
				a += 3;
			}
			for(var i=0;i<list2.Count;i++){
				if(f==1 && a > N){
					f = 2;
					a = 2;
				} else if(f==2 && a > N){
					f = 3;
					a = 1;
				}
				ans[list2[i]-1] = a;
				a += 3;
			}
		} else if(list2.Count * 3 < N){
			for(var i=0;i<list2.Count;i++){
				ans[list2[i]-1] = a;
				a += 3;
			}
			for(var i=0;i<list1.Count;i++){
				if(f==1 && a > N){
					f = 2;
					a = 2;
				} else if(f==2 && a > N){
					f = 3;
					a = 1;
				}
				ans[list1[i]-1] = a;
				a += 3;
			}
		} else {
			int a1 = 1;
			int a2 = 2;
			int a3 = 3;
			for(var i=0;i<list1.Count;i++){
				if(f==1 && a1 > N){
					f = 2;
				}
				if(f==1){
					ans[list1[i]-1] = a1;
					a1 += 3;
				} else {
					ans[list1[i]-1] = a3;
					a3 += 3;
				}
			}
			f = 1;
			for(var i=0;i<list2.Count;i++){
				if(f==1 && a2 > N){
					f = 2;
				}
				if(f==1){
					ans[list2[i]-1] = a2;
					a2 += 3;
				} else {
					ans[list2[i]-1] = a3;
					a3 += 3;
				}
			}
		}
		Console.Write(ans[0]);
		for(var i=1;i<N;i++){
			Console.Write(" "+ans[i]);
		}
	}
}