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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

/*
 * じょえチャンネル
 * 高評価・チャンネル登録よろしくおねがいします！
 * https://www.youtube.com/channel/UCRXsI3FL_kvaVL9zoolBfbQ
 */
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define all(v) v.begin(),v.end()
#define inf (int)(3e18)
#define P pair<int,int>
#define mod (int)(1e9+7)
template<class T> inline void chmin(T &a, T b) {
	a = min(a, b);
}
template<class T> inline void chmax(T &a, T b) {
	a = max(a, b);
}

int N;
vector<int>G[200005];
int co[200005];
bool used[200005];
void dfs(int x){
	used[x]=true;
	for(int i:G[x]){
		if(!used[i]){
			co[i]=co[x]*-1;
			dfs(i);
		}
	}
}
int ans[200005];
int dis[200005];
void dfs2(int x){
	used[x]=true;
	for(int i:G[x]){
		if(!used[i]){
			dis[i]=dis[x]+1;
			dfs2(i);
		}
	}
}
int dis2[200005];
void dfs3(int x){
	used[x]=true;
	for(int i:G[x]){
		if(!used[i]){
			dis2[i]=dis2[x]+1;
			dfs3(i);
		}
	}
}
set<int>v1,v2,v;
signed main(){
	cin>>N;
	rep(i,N-1){
		int a,b;cin>>a>>b;a--;b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	co[0]=1;
	dfs(0);
	int cnt1=(N+2)/3,cnt2=(N+1)/3;
	rep(i,N){
		if(co[i]==1)v1.insert(i);
		else v2.insert(i);
	}
	memset(used,false,sizeof(used));
	dfs2(0);
	int mx=0,idx;
	rep(i,N){
		if(dis[i]>mx){
			mx=dis[i];idx=i;
			dis[i]=0;
		}
	}
	memset(used,false,sizeof(used));
	dfs2(idx);
	memset(used,false,sizeof(used));
	mx=0;
	rep(i,N){
		if(dis[i]>mx){
			mx=dis[i];idx=i;
		}
	}
	dfs3(idx);
	rep(i,N){
		if(dis[i]<3&&dis2[i]<3)v.insert(i);
	}
	auto it=v1.begin();
	if(v1.size()<cnt1){
		rep(i,min(cnt1,(int)v1.size())){
			ans[*it]=1+3*i;
			v.erase(*it);
			it++;
		}
		for(int i=v1.size();i<cnt1;i++){
			if(v.empty()){
				cout<<-1<<endl;return 0;
			}
			int j=*--v.end();v.erase(j);v2.erase(j);
			ans[j]=1+3*i;
		}
		it=v2.begin();
		rep(i,min(cnt2,(int)v2.size())){
			ans[*it]=2+3*i;
			it++;
		}
	}else {
		it=v2.begin();
		rep(i,min(cnt2,(int)v2.size())){
			ans[*it]=2+3*i;
			v.erase(*it);
			it++;
		}
		for(int i=v2.size();i<cnt2;i++){
			if(v.empty()){
				cout<<-1<<endl;return 0;
			}
			int j=*--v.end();v.erase(j);v1.erase(j);
			ans[j]=2+3*i;
		}
		it=v1.begin();
		rep(i,min(cnt1,(int)v1.size())){
			ans[*it]=1+3*i;
			it++;
		}
	}

	int cnt=1;
	rep(i,N){
		if(!ans[i]){
			ans[i]=cnt*3;
			cnt++;
		}
	}
	rep(i,N)cout<<ans[i]<<" ";
	cout<<endl;
}