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)

/* lib.cpp */

#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
#include <iomanip>

#define DEBUG(x) cout << #x << " = " << (x) << ",";

using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using graph = vector<vector<int>>;

const ll MOD = 1e9 + 7;
const ld EPS = 1e-9;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int edx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int edy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

template <class T> void chmin(T &a, T b) { a = min(a, b); }
template <class T> void chmax(T &a, T b) { a = max(a, b); }

ll modpow(ll a, ll b, ll p = MOD) {
    ll ret = 1;
    while (b != 0) {
        if ((b & 1) == 1) ret *= a;
        a *= a;
        if (a >= p) a %= p;
        if (ret >= p) ret %= p;
        b >>= 1;
    }
    return ret;
}

int digit(ll x, ll base = 10) { int ret = 0; while (x) { x /= base; ret++; } return ret; }

int sumOfDigit(ll x, ll base = 10) {
    int ret = 0;
    while(x) {
        ret += x % base;
        x /= base;
    }
    return ret;
}

ll factrial(int n) { ll ret = 1; for(int i = 1;i <= n;i++) { ret *= i; } return ret; }

ll gcd(ll a, ll b) { while(b != 0) { a %= b; swap(a, b); } return a; }

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll modinv(ll a, ll p = MOD) {
    ll b = p, u = 1, v = 0;
    while(b) {
        ll tmp = a / b;
        a -= tmp * b; u -= tmp * v;
        swap(a, b); swap(u, v);
    }
    u %= p;
    if (u < 0) u += p;
    return u;
}

bool isprime(ll p) {
    if(p <= 1) return false;
    ll s = min(p - 1, (ll)ceil(sqrt(p)));
    for(int i = 2;i <= s;i++)
        if(p % i == 0) return false;
    return true;
}

ll minDivisor(ll x, ll n = 2) {
    if(x <= 1) return 1;
    ll s = min(x - 1, (ll)ceil(sqrt(x)));
    for(int i = n;i <= s;i++)
        if(x % i == 0) return i;
    return x;
}

ll countDivisor(ll x) {
    map<ll, int> m;
    if(x <= 1) return 1;
    ll s = min(x - 1, (ll)ceil(sqrt(x)));
    for(int i = 2;i <= s;i++) {
        if(x % i == 0) {
            x /= i;
            m[i]++;
            i = 1;
        }
    }
    if(x != 1) m[x]++;
    ll ret = 1;
    for(auto i : m) {
        ret *= i.second + 1;
    }
    return ret;
}

class disjoint_set
{
    vector<int> _parent, _size;

public:
    disjoint_set(int n) : _parent(n), _size(n) {
        for (int i = 0; i < n; i++) {
            _parent[i] = i;
            _size[i] = 1;
        }
    }
    int root(int x) {
        if (_parent[x] == x) return x;
        return _parent[x] = root(_parent[x]);
    }
    void unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return;
        _parent[rx] = ry;
        _size[ry] += _size[rx];
    }
    bool same(int x, int y) { return root(x) == root(y); }
    int size(int x) { return _size[root(x)]; }
};

void init() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
}

int p1, p2, p3;
int color[200000];
void dfs(const graph& g, int v, int p, int d) {
    if(d % 2 == 0) {
        if(p1 > 0) {
            color[v] = 1;
            p1--;
        }
        else if(p2 > 0) {
            color[v] = 2;
            p2--;
        }
    }
    else {
        if(p2 > 0) {
            color[v] = 2;
            p2--;
        }
        else if(p1 > 0) {
            color[v] = 1;
            p1--;
        }
    }

    for(auto i : g[v]) {
        if(i == p) continue;
        dfs(g, i, v, d + 1);
    }
}

void solve() {
    int n;
    cin >> n;
    graph g(n);
    for(int i = 0;i < n - 1;i++) {
        int a, b;
        cin >> a >> b;
        a--;b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for(int i = 0;i < n;i++) {
        color[i] = 0;
    }
    if(n % 3 == 0) {
        p1 = n / 3;
        p2 = n / 3;
    }
    if(n % 3 == 1) {
        p1 = n / 3 + 1;
        p2 = n / 3;
    }
    if(n % 3 == 2) {
        p1 = n / 3 + 1;
        p2 = n / 3 + 1;
    }

    dfs(g, 0, -1, 0);
    if(p1 != 0 || p2 != 0) {
        exit(1);
    }
    
    vector<int> v1, v2, v3;

    for(int i = 1;i <= n;i++) {
        if(i % 3 == 1) v1.push_back(i);
        else if(i % 3 == 2) v2.push_back(i);
        else v3.push_back(i);
    }

    for(int i = 0;i < n;i++) {
        if(color[i] == 1) {
                cout << v1[v1.size() - 1];
                v1.pop_back();
        } 
        else if(color[i] == 2) {
                cout << v2[v2.size() - 1];
                v2.pop_back();
        } else { 
            cout << v3[v3.size() - 1];
            v3.pop_back();
        } 
        if(i != n - 1) cout << " ";
    }
    cout << endl;
}

int main() {
    init();
    solve();
    return 0;
}