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: Runtime Error)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>//sort,reverse
#include<utility>//pair
#include<queue>//queue,priority_queue
#include<deque>//double_ended queue
using namespace std;
typedef pair<int,int> P;//Pでpair<-,->を表す。
typedef long long LL; 
LL MOD=1000000007;

/*ここから*/
int V;//頂点の個数
vector<int> e[200010];//iから出る辺
int t[200010];//親情報の記録媒体
int h[200010];
bool h_flg[200010];

//すでにある辺と頂点の情報から木の作成
void Tree(int s){//sは一番根元
    for(int i=1;i<=V;i++){
        t[i]=i;//初期情報
    }
    queue <int> q;
    q.push(s);
    while(!q.empty()){
        int from;
        from=q.front();
        q.pop();
        while(!e[from].empty()){
            int to;
            to=e[from].back();
            e[from].pop_back();
            if(to!=t[from]){
                t[to]=from;
                q.push(to);
            }
        }
    }
}

void height(int s){
    if(t[s]==s){h[s]=1;h_flg[s]=1;}
    else{
        if(!h_flg[t[s]]){height(s);}
        h[s]=h[t[s]]+1;
        h_flg[s]=true;
    }
}

/*ここまで*/
int main()
{ 
    int n;
    cin>>n;
    V=n;
    for(int i=0;i<V-1;i++){
        int a,b;
        cin>>a>>b;
        e[a].push_back(b);
        e[b].push_back(a);
    }
    Tree(1);
    for(int i=0;i<200010;i++){
        h[i]=0;
        h_flg[i]=false;
    }
    for(int i=1;i<=V;i++){
        height(i);
    }
    //for(int i=1;i<=V;i++){cout<<h[i]<<endl;}
    int ans[n+1];
    int cnt1=0;
    int cnt2=0;
    int b1=0;
    int b2=0;
    if(n%3==0){b1=n/3;b2=n/3;}
    else if(n%3==1){b1=n/3+1;b2=n/3;}
    else{b1=n/3+1;b2=n/3+1;}
    for(int i=1;i<=n;i++){
        if(h[i]%2==1){
            cnt1++;
            if(cnt1<=b1){ans[i]=1;}
            else{ans[i]=3;}
        }
        else{
            cnt2++;
            if(cnt2<=b2){ans[i]=2;}
            else{ans[i]=3;}
            }
    }
    if(cnt1<b1){
        int x=cnt1;
        cnt1=0;
        cnt2=0;
        int cnt3=x;
        for(int i=1;i<=n;i++){
            if(ans[i]==1){ans[i]=3*x;x--;}
            else{
                if(cnt1<b1){ans[i]=3*cnt1+1;cnt1++;}
                else if(cnt2<b2){ans[i]=3*cnt2+2;cnt2++;}
                else{ans[i]=cnt3*3+3;cnt3++;}
            }
        }
    }
    else if(cnt2<b2){
        int x=cnt2;
        cnt1=0;
        cnt2=0;
        int cnt3=x;
        for(int i=1;i<=n;i++){
            if(ans[i]==2){ans[i]=3*x;x--;}
            else{
                if(cnt1<b1){ans[i]=3*cnt1+1;cnt1++;}
                else if(cnt2<b2){ans[i]=3*cnt2+2;cnt2++;}
                else{ans[i]=cnt3*3+3;cnt3++;}
            }
        }
    }
    else{
        cnt1=0;cnt2=0;
        int cnt3=0;
        for(int i=1;i<=n;i++){
            if(ans[i]==1){ans[i]=cnt1*3+1;cnt1++;}
            else if(ans[i]==2){ans[i]=cnt2*3+2;cnt2++;}
            else{ans[i]=cnt3*3+3;cnt3++;}
        }
    }
    for(int i=1;i<n;i++){
        cout<<ans[i]<<' ';
    }
    cout<<ans[n]<<endl;
    return 0;
}