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: Accepted)

#include <bits/stdc++.h>
using namespace std;
# define rep(i,n) for(int i=0;i<(n);i++)
typedef long long ll;


int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);
	
	ll N;
	cin>>N;
    ll a,b;
    vector<deque<ll>> dq(N);
    rep(i,N-1){
        cin>>a>>b;
        --a;
        --b;
        dq[a].push_back(b);
        dq[b].push_back(a);
    }
    vector<ll> c_ar(N,-1);
    deque<ll> q,next;
    q.push_back(0);
    next.push_back(1);
    c_ar[0] = 0;
    ll n_1 = 0;
    ll c;
    while(!q.empty()){
        a = q.front();q.pop_front();
        if(c_ar[a]==0){
            c = 1;
        }else{
            c = 0;
        }
        for(auto v : dq[a]){
            if(c_ar[v]== -1){
                c_ar[v] =c;
                q.push_back(v);
                if(c==1) ++n_1;
            }
        }
    }
    // rep(i,N){
    //     cout<<c_ar[i]<<" ";
    // }
    // cout<<n_1;
    // cout<<"\n";

    //塗り終わったはず
    vector<ll> ans(N);
    deque<ll> p1,p2,p0;
    rep(i,N){
        if((i+1)%3==1){
            p1.push_back(i+1);
        }else if((i+1)%3==2){
            p2.push_back(i+1);
        }else{
            p0.push_back(i+1);
        }
    }

    ll tmp_ans;
    if(n_1 <= N/3){
        rep(i,N){
            if(c_ar[i] ==1){
                ans[i] = p0.front();
                p0.pop_front();
            }else{
                if(!p1.empty()){
                    ans[i] = p1.front();
                    p1.pop_front();
                }else if(!p2.empty()){
                    ans[i] = p2.front();
                    p2.pop_front();
                }else{
                    ans[i] = p0.front();
                    p0.pop_front();  
                }
            }
        }

    }else if(N-n_1<=N/3){
        rep(i,N){
            if(c_ar[i] !=1){
                ans[i] = p0.front();
                p0.pop_front();
            }else{
                if(!p1.empty()){
                    ans[i] = p1.front();
                    p1.pop_front();
                }else if(!p2.empty()){
                    ans[i] = p2.front();
                    p2.pop_front();
                }else{
                    ans[i] = p0.front();
                    p0.pop_front();  
                }
            }
        }
    }else{
        rep(i,N){
            if(c_ar[i] ==1){
                if(!p2.empty()){
                    ans[i] = p2.front();
                    p2.pop_front();
                }else{
                    ans[i] = p0.front();
                    p0.pop_front();  
                }
            }else{
                if(!p1.empty()){
                    ans[i] = p1.front();
                    p1.pop_front();
                }else{
                    ans[i] = p0.front();
                    p0.pop_front();  
                }
            }
        }
    }
    rep(i,N-1){
        cout<<ans[i]<<" ";
    }
    cout<<ans[N-1]<<"\n";
	return 0;
}