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>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <cctype>
#include <numeric>
#include <bitset>
#include <functional>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};
constexpr int N_MAX = 200000;

vector<int> tree[N_MAX];
vector<int> used(N_MAX, 0);
vector<int> color(N_MAX, 0);
vector<int> cnt(6, 0);
int n;

void dfs(int node, int par, int c) {
    color[node] = c;
    ++cnt[c];
    for(int child: tree[node]) {
        if(child == par) continue;
        dfs(child, node, (c==5?0:c+1));
    }
}

void dfs2(int node, int par) {
}

pair<int, ll> dfs_diameter(int node, ll dist) {
    used[node] = 1;
    pair<int, ll> ret = {node, dist};
    for(int next_node: tree[node]) {
        if(used[next_node]) continue;
        pair<int, ll> q = dfs_diameter(next_node, dist+1);
        if(ret.second < q.second) {
            ret = q;
        }
    }
    return ret;
}

ll calc_diameter() {
    pair<int, ll> u = dfs_diameter(0, 0);
    used = vector<int>(N_MAX, 0);
    return dfs_diameter(u.first, 0).second;
}

bool paint(int k, int sw) {
    vector<int> ans(n, 0);
    int j[3] = {1, 0, 0};
    for(int i=0;i<n;++i) {
        if(((color[i] % 3) + k) % 3 != 0) {
            int m = ((color[i] % 3) + k) % 3;
            if(j[m] >= (n+3-m)/3) {
                if(j[0]-1 >= n/3) {
                    return false;
                } else {
                    ans[i] = j[0] * 3;
                    ++j[0];
                }
            } else {
                ans[i] = j[m] * 3 + m;
                ++j[m];
            }
        } else {
            if((color[i] < 3 && sw == 0) || (color[i] >= 3 && sw == 1)) continue;
            if(j[0]-1 >= n/3) {
                return false;
            } else {
                ans[i] = j[0] * 3;
                ++j[0];
            }
        }
    }
    for(int i=0;i<n;++i) {
        if((color[i] < 3 && sw == 0) || (color[i] >= 3 && sw == 1)) {
            if(j[2] < (n+1)/3) {
                ans[i] = j[2] * 3 + 2;
                ++j[2];
            } else if(j[1] < (n+2)/3) {
                ans[i] = j[1] * 3 + 1;
                ++j[1];
            } else if(j[0]-1 < n/3) {
                ans[i] = j[0] * 3;
                ++j[0];
            } else {
                return false;
            }
        }
    }

    cout << ans[0];
    for(int i=1;i<n;++i) cout << " " << ans[i];
    cout << endl;

    return true;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int x, y;
    cin >> n;
    for(int i=0;i<n-1;++i) {
        cin >> x >> y;
        --x; --y;
        tree[x].push_back(y);
        tree[y].push_back(x);
    }

    int diameter = calc_diameter();
    if(diameter < 3) {
        cout << 1;
        for(int i=1;i<n;++i) cout << " " << i+1;
        cout << endl;
        return 0;
    }

    int leaf = -1;
    for(int i=0;i<n&&leaf==-1;++i) if(tree[i].size() == 1) leaf = i;
    dfs(leaf, -1, 0);

    for(int k=0;k<3;++k) {
        if(paint(k, 0)) return 0;
        if(paint(k, 1)) return 0;
    }


    cout << -1 << endl;
    return 0;
}