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 <bits/stdc++.h>
#define rep(i,cc,n) for(int i=cc;i<=n;++i)
#define drep(i,cc,n) for(int i=cc;i>=n;--i)
#define sz(s) (int)(s.size())
#define vecprint(v) rep(i,0,v.size()-1)cout << v[i] << " ";cout << endl;
#define arrprint(v,n) rep(i,0,n-1)cout << v[i] << " ";cout << endl;
#define matprint(v,n,m) rep(i,0,n-1){rep(j,0,m-1){cout << v[i][j] << " ";}cout << endl;}
using namespace std;
const int mod=1000000007;
const int INF=1001001001;
typedef long long ll;
typedef pair<int,int> P;

vector<int> to[100005],to2[100005];

void dfs(int start,int node, int par=-1,int dist = 0){
    if(dist==3){
        to2[start].push_back(node);
        to2[node].push_back(start);
        return;
    }
    for(int ch : to[node]){
        if(ch == par)continue;
        dfs(start,ch,node,dist+1);
    }
    return;
}

int main(){
    int n;
    cin >> n;
    rep(i,0,n-2){
        int a,b;
        cin >> a >> b;
        a--;b--;
        to[a].push_back(b);
        to[b].push_back(a);
    }
    vector<int> p(n);
    rep(i,0,n-1){
        dfs(i,i);
    }
    vector<int> ans(n);
    rep(i,0,n-1)ans[i] = -1;
    int cnt = 1;
    stack<int> s;
    rep(i,0,n-1){
        if(sz(to2[i])>0){
            queue<int> q;
            q.push(i);
            ans[i] = cnt;
            cnt++;
            while(!q.empty()){
                int node = q.front();q.pop();
                for(int ch : to2[node]){
                    if(ans[ch] != -1)continue;
                    if((cnt%3)*(ans[node]%3) != 0 && (cnt%3)+(ans[node]%3) != 3){s.push(cnt);cnt++;}
                    ans[ch] = cnt;
                    cnt++;
                    q.push(ch);
                }
            }
            break;
        }
    }
    rep(i,0,n-1){
        if(ans[i] == -1){
            if(cnt <= n){
                ans[i] = cnt;
                cnt++;
            }else{
                ans[i] = s.top();
                s.pop();
            }
        }
    }
    rep(i,0,n-1)cout << ans[i] << endl;



    // cout << ans << endl;
//  printf("%.10f\n",ans);

    return 0;
}