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();
		
		List<int>[] E2 = new List<int>[N];
		for(int i=0;i<N;i++){
			E2[i] = new List<int>();
		}
		for(int i=0;i<N;i++){
			if(anc(i, 2) != -1){
				E2[i].Add(anc(i, 2));
				E2[anc(i, 2)].Add(i);
			}
		}
		
		List<int>[] E3 = new List<int>[N];
		for(int i=0;i<N;i++){
			E3[i] = new List<int>();
		}
		for(int i=0;i<N;i++){
			if(anc(i, 3) != -1){
				E3[i].Add(anc(i, 3));
				E3[anc(i, 3)].Add(i);
			}
		}
		
		int[] ddep = new int[N];
		for(int i=0;i<N;i++) ddep[i] = -1;
		int[] prev = new int[N];
		for(int i=0;i<N;i++) prev[i] = -1;
		
		for(int i=0;i<N;i++){
			if(ddep[i] != -1) continue;
			ddep[i] = 0;
			var q = new Queue<int>();
			q.Enqueue(i);
			while(q.Count > 0){
				var now = q.Dequeue();
				foreach(var nxt in E3[now]){
					if(ddep[nxt] == -1){
						ddep[nxt] = ddep[now] + 1;
						prev[nxt] = now;
						q.Enqueue(nxt);
					}
				}
				if(anc(now,1) != -1){
					int par = anc(now, 1);
					foreach(var nxt in E2[par]){
						if(ddep[nxt] == -1){
							if(parent[0][nxt] == now) continue;
							ddep[nxt] = ddep[now] + 1;
							prev[nxt] = now;
							q.Enqueue(nxt);
						}
					}
				}
				if(anc(now,2) != -1){
					int par = anc(now, 2);
					foreach(var nxt in E[par]){
						if(ddep[nxt] == -1){
							if(parent[0][now] == nxt) continue;
							ddep[nxt] = ddep[now] + 1;
							prev[nxt] = now;
							q.Enqueue(nxt);
						}
					}
				}
			}
		}
		
		Pair[] P = new Pair[N];
		for(int i=0;i<N;i++){
			P[i] = new Pair(i, ddep[i]);
		}
		Array.Sort(P, (x, y) => x.Dep.CompareTo(y.Dep));
		//Console.WriteLine(String.Join(" ",ddep));
		
		var num = new Queue<int>[3];
		for(int i=0;i<3;i++) num[i] = new Queue<int>();
		for(int i=1;i<=N;i++){
			num[i % 2].Enqueue(i);
		}
		
		Func<int,int> Take = n => {
			if(n <= 0){
				if(num[1].Count > 0){
					var ret = num[1].Dequeue();
					return ret;
				}
				if(num[2].Count > 0){
					var ret = num[2].Dequeue();
					return ret;
				}
				if(num[0].Count > 0){
					var ret = num[0].Dequeue();
					return ret;
				}
				return -1;
			}
			if(n == 1){
				if(num[2].Count > 0){
					var ret = num[2].Dequeue();
					return ret;
				}
				if(num[0].Count > 0){
					var ret = num[0].Dequeue();
					return ret;
				}
				return -1;
			}
			if(n == 2){
				if(num[1].Count > 0){
					var ret = num[1].Dequeue();
					return ret;
				}
				if(num[0].Count > 0){
					var ret = num[0].Dequeue();
					return ret;
				}
				return -1;
			}
			return -1;
		};
		
		int[] ans = new int[N];
		foreach(var x in P){
			int pos = x.Idx;
			//Console.WriteLine(pos);
			if(prev[pos] == -1){
				ans[pos] = Take(-1);
				continue;
			}
			int pans = ans[prev[pos]];
			ans[pos] = Take(pans % 3);
		}
		
		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));}
}