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 <algorithm>
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <unordered_map>
#include <set>
#include <map>
#define rep(i,n) for (int i=0; i<n;++i)
#define ALL(a)  (a).begin(),(a).end()
#define PI 3.14159265358979
typedef long long ll;
using namespace std;
const ll MOD = 1e9+7LL;
const int INF = 2e9;
int N;
vector<int> Tree[200002];
int ans[200002];
int guuki[200002];
typedef pair<int, int> P;

/*
「すべての頂点の組 (i,j)について、頂点 iと頂点jの距離が3であるならば、
piと pjの和または積が3の倍数である。」の対偶は、
「[pi≡1かつpj≡1]または[pi≡2かつpj≡2](mod3)ならば、iとjの距離は3でない」
である。
頂点1からの距離が偶奇でわけたときに、3の距離となるときは頂点が偶と奇。
*/
void dfs(int idx, int mod2) { // mod2は偶奇
  if (guuki[idx] >= 0) return;
  guuki[idx] = mod2;
  mod2=mod2==1?0:1;
  rep(i, Tree[idx].size()) {
    int next = Tree[idx][i];
    if (guuki[next]<0) dfs(next, mod2);
  }
}

bool assign_and_add(int idx, int &cnt) {
  if (cnt>N) return false;
  ans[idx] = cnt;
  cnt+=3;
  return true;
}

void calc(bool odd_flag, bool even_flag) {
  int idx_1 = 1; // mod3
  int idx_2 = 2; // mod3
  int idx_0 = 3; // mod3
  rep(i, N) {
    if (odd_flag) {
      if (guuki[i]) assign_and_add(i, idx_0);
      else {
        if (!assign_and_add(i, idx_1)) {
          assign_and_add(i, idx_2);
        }
      }  
    } else if (even_flag) {
      if (!guuki[i]) assign_and_add(i, idx_0);
      else {
        if (!assign_and_add(i, idx_1)) {
          assign_and_add(i, idx_2);
        }
      }
    } else {
      if (guuki[i]) {
        if (!assign_and_add(i, idx_1)) {
          assign_and_add(i, idx_0);
        }
      } else {
        if (!assign_and_add(i, idx_2)) {
          assign_and_add(i, idx_0);
        }
      }
    }
  }
}

int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> N;
  int a,b;
  rep(i, N-1) {
    cin >> a >> b;
    a--;b--;
    Tree[a].push_back(b);
    Tree[b].push_back(a);
  }
  fill(guuki, guuki+N, -1);
  dfs(0, 0);
  int even = 0;
  int odd = 0;
  rep(i, N) {
    if (guuki[i]) odd++;
    else even++;
  }
  bool odd_flag = odd<=N/3; // 距離が偶数の頂点がN/3以下
  bool even_flag = even<=N/3; // 距離が奇数の頂点がN/3以下
  calc(odd_flag, even_flag);
  rep(i, N) {
    cout << ans[i] << ' ';
  }
  cout << endl;
  return 0;
}