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

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
		
	void lca_init(){
		parent = new int[lmax][];
		for(int i=0;i<lmax;i++){
			parent[i]=new int[N];
		}
		depth=new int[N];
		
		root=0;
		//dfs(root,-1,0);//深さを数えるdfs
		bfs(root,0);//深さを数えるbfs
		
		for(int i=0;i<lmax-1;i++){
			for(int j=0;j<N;j++){
				if(parent[i][j]<0){
					parent[i+1][j] = -1;
				}else{
					parent[i+1][j] = parent[i][parent[i][j]];
				}
			}
		}
		
	}
	void bfs(int strt,int dep_strt){
		Queue<int> Q = new Queue<int>();
		parent[0][strt] = -1;
		depth[strt] = 0;
		Q.Enqueue(strt);
		while(Q.Count>0){
			int now=Q.Dequeue();
			int par=parent[0][now];
			foreach(int v in E[now]){
				if(v!=par){
					parent[0][v]=now;depth[v]=depth[now]+1;
					Q.Enqueue(v);
				}
			}
		}
	}

	List<int>[] E;
	int[][] parent;
	int[] depth;
	static int lmax=4;
	int root;
	
	int anc(int idx, int h){
		//Console.WriteLine("{0} {1}",idx, h);
		if(h == 1) return parent[0][idx];
		if(h == 2) return parent[1][idx];
		if(parent[1][idx] == -1) return -1;
		if(h == 3) return parent[0][parent[1][idx]];
		return -1;
	}
	
	public void Solve(){
		E = new List<int>[N];
		for(int i=0;i<N;i++) E[i] = new List<int>();
		for(int i=0;i<N-1;i++){
			E[A[i]].Add(B[i]);
			E[B[i]].Add(A[i]);
		}
		lca_init();
		
		int[] ans = new int[N];
		List<int>[] bw = new List<int>[2];
		for(int i=0;i<2;i++) bw[i] = new List<int>();
		for(int i=0;i<N;i++){
			bw[depth[i] % 2].Add(i);
		}
		
		int[] cnt = new int[3];
		cnt[0] = N / 3;
		cnt[1] = N / 3 + (N % 3 != 0 ? 1 : 0);
		cnt[2] = N / 3 + (N % 3 == 2 ? 1 : 0);
		Array.Sort(bw, (x, y) => x.Count.CompareTo(y.Count));
		Queue<int>[] q = new Queue<int>[3];
		for(int i=0;i<3;i++) q[i] = new Queue<int>();
		for(int i=1;i<=N;i++) q[i % 3].Enqueue(i);
		
		if(bw[0].Count < cnt[2]){
			foreach(var n in bw[0]){
				ans[n] = q[0].Dequeue();
			}
			foreach(var n in bw[1]){
				int v = -1;
				if(q[0].Count > 0) v = q[0].Dequeue();
				else if(q[1].Count > 0) v = q[1].Dequeue();
				else if(q[2].Count > 0) v = q[2].Dequeue();
				ans[n] = v;
			}
			
		} else {
			foreach(var n in bw[0]){
				int v = -1;
				if(q[2].Count > 0) v = q[2].Dequeue();
				else if(q[0].Count > 0) v = q[1].Dequeue();
				ans[n] = v;
			}
			foreach(var n in bw[1]){
				int v = -1;
				if(q[1].Count > 0) v = q[1].Dequeue();
				else if(q[0].Count > 0) v = q[0].Dequeue();
				ans[n] = v;
			}
		}
		
		
		Console.WriteLine(String.Join(" ",ans));
		
	}
	
	class Pair{
		public int Idx,Dep;
		public Pair(int idx, int dep){
			Idx = idx; Dep = dep;
		}
	}
	
	int N;
	int[] A,B;
	public Sol(){
		N = ri();
		A = new int[N-1];
		B = new int[N-1];
		for(int i=0;i<N-1;i++){
			var d = ria();
			A[i] = d[0] - 1;
			B[i] = d[1] - 1;
		}
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}