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)

#include<iostream>
#include<vector>
using namespace std;
typedef long long li;
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }
// Container コンテナ型, map以外
template< template<class ...> class Ctn,class T>
std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
  auto itr=v.begin();
  while(itr!=v.end()){
    if(itr!=v.begin())cout << " ";
    cout << *(itr++);
  }
  return os;
}

void dfs(vector<vector<int>>& adj,vector<vector<int>>& col,
         int p=-1,int a=0){
  int n=adj.size();
  static vector<int> seen(n,0);
  static int c=0;
  seen[a]=1;
  col[c].push_back(a);
  c^=1;
  for(int x:adj[a]){
    if(x==p) continue;
    dfs(adj,col,a,x);
  }
  c^=1;
}

void balance(vector<int>& p,vector<vector<int>> col){
  int n=p.size();
  int a=0,b=1;
  if(col[0].size()<col[1].size()){ a=1;b=0; }

  rep(i,n){
    if(i%3==0){
      p[*col[a].rbegin()]=i+1;
      col[a].pop_back();
    }else if(i%3==1){
      p[*col[b].rbegin()]=i+1;
      col[b].pop_back();
    }else{
      int c=(col[0].size()<=col[1].size());
      p[*col[c].rbegin()]=i+1;
      col[c].pop_back();
    }
  }
}

void umbalance(vector<int>& p,vector<vector<int>> col){
  int n=p.size();
  int a=0,b=1;
  if(col[0].size()<col[1].size()){ a=1;b=0; }
  int c=3;
  while(c<=n){
    p[*col[b].rbegin()]=c;
    col[b].pop_back();
    c+=3;
    if(col[b].size()==0) b=a;
  }
  c=1;
  for(int x: col[a]){
    p[x]=c;
    c+=c%3;
  }
}

int main(){
  int n; cin >>n;
  vector<vector<int>> adj(n);
  rep(i,n-1){
    int a,b; cin >>a >>b; a--; b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  vector<vector<int>> col(2);
  dfs(adj,col);
  
  vector<int> p(n);
  int a=0,b=1;
  if(col[0].size()<col[1].size()){ a=1;b=0; }
  if(col[a].size()<col[b].size()*2){
    balance(p,col);
  }else{
    umbalance(p,col);
  }
  print(p);
}