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: Accepted)

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
//#define int ll

const int N = 2e5 + 10;
vector<int> g[N];
int h[N], ans[N];

void dfs(int v, int par) {
  h[v] = h[par] ^ 1;
  for (auto u : g[v]) {
    if (u != par) {
      dfs(u, v);
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    g[a].pb(b);
    g[b].pb(a);
  }
  dfs(1, 0);
  vector<int> cnt(2);
  for (int i = 1; i <= n; i++) {
    ans[i] = h[i] + 1;
    cnt[h[i]]++;
  }
  vector<int> have(2);
  have[0] = (n + 2) / 3;
  have[1] = (n + 1) / 3;
  vector<int> cur(4);
  iota(all(cur), 0);
  if (cnt[0] < have[0] || cnt[1] < have[1]) {
    swap(cnt[0], cnt[1]);
    for (int i = 1; i <= n; i++) {
      h[i] ^= 1;
      ans[i] = h[i] + 1;
    }
    if (cnt[0] < have[0] || cnt[1] < have[1]) {
      assert(cnt[0] >= have[0] || cnt[1] >= have[1]);
      bool kek = (cnt[1] < have[1]);
      for (int i = 1; i <= n; i++) {
        if (kek) {
          h[i] ^= 1;
        }
        if (!h[i]) {
          cout << cur[3] << ' ';
          cur[3] += 3;
        }
        else {
          if (have[0] > 0) {
            cout << cur[1] << ' ';
            cur[1] += 3;
            have[0]--;
          }
          else if (have[1] > 0) {
            cout << cur[2] << ' ';
            cur[2] += 3;
            have[1]--;
          }
          else {
            cout << cur[3] << ' ';
            cur[3] += 3;
          }
        }
      }
      assert(have[0] == 0);
      assert(have[1] == 0);
      cout << '\n';
      exit(0);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[h[i]] > have[h[i]]) {
      ans[i] = 3;
      cnt[h[i]]--;
    }
  }
  assert(cnt[0] == have[0]);
  assert(cnt[1] == have[1]);
  for (int i = 1; i <= n; i++) {
    cout << cur[ans[i]] << ' ';
    cur[ans[i]] += 3;
  }
  cout << '\n';
}