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)

//ThREE
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define pb push_back
const int MAXN = 2e5 + 7;
vector<int> G[MAXN], p0, p1, p2;
int n, n1=0, n2=0, n0=0, c[MAXN], dist[MAXN],a0=0,a1=0,a2=0;

int main(){
    fastio; cin>>n;

    for(int i=1; i<=n; i++){
        c[i] = -1; 
        dist[i] = MAXN;
        if(i%3 == 0){p0.pb(i); n0++;}
        else if(i%3 == 1){p1.pb(i); n1++;}
        else if(i%3 == 2){p2.pb(i); n2++;}
    }
    /*
    cout<<"SACA :";
    for(int i=0; i<(int)p0.size(); i++){
        cout<<p0[i]<<" ";
    }
    cout<<endl;

    cout<<"SACA :";
    for(int i=0; i<(int)p1.size(); i++){
        cout<<p1[i]<<" ";
    }
    cout<<endl;

    cout<<"SACA :";
    for(int i=0; i<(int)p2.size(); i++){
        cout<<p2[i]<<" ";
    }
    cout<<endl;*/

    for(int i=1; i<n; i++){
        int ai,bi;
        cin>>ai>>bi;
        G[ai].push_back(bi);
        G[bi].push_back(ai);
    }

    queue<int> q; q.push(1);
    dist[1] = 0;
    c[1] = 0;
    int max_dist = 0;

    while(!q.empty()){
        int u = q.front();
        max_dist = max(max_dist, dist[u]);
        q.pop();
        for(int i=0; i<(int)G[u].size(); i++){
            int viz = G[u][i];
            if(dist[viz] > dist[u] + 1){
                dist[viz] = dist[u] + 1;
                c[viz] = (c[u] + 1)%2;
                q.push(viz);
            }
        }
    }
    /*for(int i=1; i<=n; i++){
        cout<<"JMJ "<<i<<" "<<dist[i]<<" "<<c[i]<<endl;
    }*/
    for(int i=1; i<=n; i++){
        if(c[i] == 0){
            if(a1 < n1){c[i]=p1[a1]; a1++;}
            else if(a0 < n0){c[i]=p0[a0]; a0++;}
            else if(max_dist < 3 && a2<n2){c[i]=p2[a2];a2++;}
        }else{
            if(a2 < n2){c[i]=p2[a2]; a2++;}
            else if(a0 < n0){c[i]=p0[a0]; a0++;/*cout<<"AUI"<<endl;*/}
            else if(max_dist < 3 && a1<n1){c[i]=p1[a1]; a1++;}
        }
        //cout<<"DECIDIDO "<<c[i]<<endl;
    }
    if(a1<n1 || a2<n2){cout<<"-1"<<endl; return 0;}
    for(int i=1; i<=n; i++){
        if(i > 1)cout<<" ";
        cout<<c[i];
    }
}