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 <bits/stdc++.h>
using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using Edge = pair<int, ll>;
using Graph = vector<vector<int>>;
using WeightedGraph = vector<vector<Edge>>;
const ll INF64 = 1LL << 60;
const int INF32 = 1 << 29;
const ll MOD = 1000000007;

const int CASES = 1;

struct LCA {
    vector<vector<int> > parent; // parent[d][v] := 2^d-th parent of v
    vector<int> depth;
    LCA() { }
    LCA(const Graph &G, int r = 0) { init(G, r); }
    void init(const Graph &G, int r = 0) {
        int V = (int)G.size();
        int h = 1;
        while ((1<<h) < V) ++h;
        parent.assign(h, vector<int>(V, -1));
        depth.assign(V, -1);
        dfs(G, r, -1, 0);
        for (int i = 0; i+1 < (int)parent.size(); ++i)
            for (int v = 0; v < V; ++v)
                if (parent[i][v] != -1)
                    parent[i+1][v] = parent[i][parent[i][v]];
    }
    void dfs(const Graph &G, int v, int p, int d) {
        parent[0][v] = p;
        depth[v] = d;
        for (auto e : G[v]) if (e != p) dfs(G, e, v, d+1);
    }
    int get(int u, int v) {
        if (depth[u] > depth[v]) swap(u, v);
        for (int i = 0; i < (int)parent.size(); ++i)
            if ( (depth[v] - depth[u]) & (1<<i) )
                v = parent[i][v];
        if (u == v) return u;
        for (int i = (int)parent.size()-1; i >= 0; --i) {
            if (parent[i][u] != parent[i][v]) {
                u = parent[i][u];
                v = parent[i][v];
            }
        }
        return parent[0][u];
    }
};


// Sparse Table
template<class MeetSemiLattice> struct SparseTable {
    vector<vector<pair<MeetSemiLattice,int> > > dat;
    vector<int> height;

    SparseTable() { }
    SparseTable(const vector<MeetSemiLattice> &vec) { init(vec); }
    void init(const vector<MeetSemiLattice> &vec) {
        int n = (int)vec.size(), h = 0;
        while ((1<<h) < n) ++h;
        dat.assign(h, vector<pair<MeetSemiLattice,int> >(1<<h));
        height.assign(n+1, 0);
        for (int i = 2; i <= n; i++) height[i] = height[i>>1]+1;
        for (int i = 0; i < n; ++i) dat[0][i] = {vec[i], i};
        for (int i = 1; i < h; ++i)
            for (int j = 0; j < n; ++j)
                dat[i][j] = min(dat[i-1][j], dat[i-1][min(j+(1<<(i-1)),n-1)]);
    }

    pair<MeetSemiLattice,int> get(int a, int b) {
        return min(dat[height[b-a]][a], dat[height[b-a]][b-(1<<height[b-a])]);
    }
};

// Segment Tree
template<class Monoid, class Action> struct SegTree {
    using FuncMonoid = function< Monoid(Monoid, Monoid) >;
    using FuncAction = function< void(Monoid&, Action) >;
    using FuncLazy = function< void(Action&, Action) >;
    FuncMonoid FM;
    FuncAction FA;
    FuncLazy FL;
    Monoid UNITY_MONOID;
    Action UNITY_LAZY;
    int SIZE, HEIGHT;
    vector<Monoid> dat;
    vector<Action> lazy;

    SegTree() { }
    SegTree(int n, const FuncMonoid fm, const FuncAction fa, const FuncLazy fl,
            const Monoid &unity_monoid, const Action &unity_lazy)
            : FM(fm), FA(fa), FL(fl), UNITY_MONOID(unity_monoid), UNITY_LAZY(unity_lazy) {
        SIZE = 1; HEIGHT = 0;
        while (SIZE < n) SIZE <<= 1, ++HEIGHT;
        dat.assign(SIZE * 2, UNITY_MONOID);
        lazy.assign(SIZE * 2, UNITY_LAZY);
    }
    void init(int n, const FuncMonoid fm, const FuncAction fa, const FuncLazy fl,
              const Monoid &unity_monoid, const Action &unity_lazy) {
        FM = fm; FA = fa; FL = fl;
        UNITY_MONOID = unity_monoid; UNITY_LAZY = unity_lazy;
        SIZE = 1; HEIGHT = 0;
        while (SIZE < n) SIZE <<= 1, ++HEIGHT;
        dat.assign(SIZE * 2, UNITY_MONOID);
        lazy.assign(SIZE * 2, UNITY_LAZY);
    }

    /* set, a is 0-indexed */
    void set(int a, const Monoid &v) { dat[a + SIZE] = v; }
    void build() {
        for (int k = SIZE - 1; k > 0; --k)
            dat[k] = FM(dat[k*2], dat[k*2+1]);
    }

    /* update [a, b) */
    inline void evaluate(int k) {
        if (lazy[k] == UNITY_LAZY) return;
        if (k < SIZE) FL(lazy[k*2], lazy[k]), FL(lazy[k*2+1], lazy[k]);
        FA(dat[k], lazy[k]);
        lazy[k] = UNITY_LAZY;
    }
    inline void update(int a, int b, const Action &v, int k, int l, int r) {
        evaluate(k);
        if (a <= l && r <= b)  FL(lazy[k], v), evaluate(k);
        else if (a < r && l < b) {
            update(a, b, v, k*2, l, (l+r)>>1), update(a, b, v, k*2+1, (l+r)>>1, r);
            dat[k] = FM(dat[k*2], dat[k*2+1]);
        }
    }
    inline void update(int a, int b, const Action &v) { update(a, b, v, 1, 0, SIZE); }

    /* get [a, b) */
    inline Monoid get(int a, int b, int k, int l, int r) {
        evaluate(k);
        if (a <= l && r <= b)
            return dat[k];
        else if (a < r && l < b)
            return FM(get(a, b, k*2, l, (l+r)>>1), get(a, b, k*2+1, (l+r)>>1, r));
        else
            return UNITY_MONOID;
    }
    inline Monoid get(int a, int b) { return get(a, b, 1, 0, SIZE); }
    inline Monoid operator [] (int a) { return get(a, a+1); }

    /* debug */
    void print() {
        for (int i = 0; i < SIZE; ++i) { cout << (*this)[i]; if (i != SIZE) cout << ","; }
        cout << endl;
    }
};

// Euler Tour
using Node = pair<long long, int>;
const auto fm = [](Node a, Node b) { return Node(a.first + b.first, a.second + b.second); };
const auto fa = [](Node &a, long long d) { a.first += d * a.second; };
const auto fl = [](long long &d, long long e) { d += e; };

struct EulerTour {
    // main results
    Graph tree;
    vector<int> depth;
    vector<int> node; // the node-number of i-th element of Euler-tour
    vector<int> vf, ve; // the index of Euler-tour of node v
    vector<int> eid; // the index of edge e (i*2 + (0: dir to leaf, 1: dir to root))

    // sub results
    SparseTable<int> st; // depth (to find LCA)

    // segtree
    SegTree<Node, long long> seg;

    // initialization
    EulerTour(const Graph &tree_) { init(tree_); }
    void init(const Graph &tree_) {
        tree = tree_;
        int V = (int)tree.size();
        depth.resize(V*2-1); node.resize(V*2-1); vf.resize(V); ve.resize(V); eid.resize((V-1)*2);
        seg.init((V-1)*2, fm, fa, fl, Node(0, 0), 0);
        int k = 0;
        dfs(0, -1, 0, k);
        st.init(depth);
        seg.build();
    }

    void dfs(int v, int par, int dep, int &ord) {
        node[ord] = v;
        depth[ord] = dep;
        vf[v] = ve[v] = ord;
        ++ord;
        for (auto e : tree[v]) {
            if (e == par) continue;
            seg.set(ord-1, Node(0, 1));
            dfs(e, v, dep+1, ord);
            node[ord] = v;
            depth[ord] = dep;
            ve[v] = ord;
            seg.set(ord-1, Node(0, -1));
            ++ord;
        }
    }

    inline int LCA(int u, int v) {
        int a = vf[u], b = vf[v];
        if (a > b) swap(a, b);
        return node[st.get(a, b+1).second];
    }

    inline void update(int v, long long x) {
        seg.update(vf[v], ve[v], x);
    }

    inline long long get(int v) {
        return seg.get(0, vf[v]).first;
    }

    inline long long get(int u, int v) {
        int lca = LCA(u, v);
        return get(u) + get(v) - get(lca)*2;
    }
};


int main() {
    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);
    }

    LCA lca(G);
    vector<vector<int>> vec(N+1);
    for(int i = 0; i < N; ++i){
        vec[lca.depth[i]].push_back(i);
    }
    set<pint> p;
    //same tree
    for(int i = 0; i+3 < N; ++i){
        int j = i + 3;
        int m1 = vec[i].size();
        int m2 = vec[j].size();
        for(int k = 0; k < m1; ++k){
            for(int l = 0; l < m2; ++l){
                int t1 = vec[i][k];
                int t2 = vec[j][l];
                int c = lca.get(t1, t2);
                if(3 == lca.depth[t1]+ lca.depth[t2] - 2*lca.depth[c]){
                    if(t1 > t2) swap(t1,t2);
                    p.insert({t1, t2});
                }
            }
        }
    }
    // other tree
    for(int i = 0; i+1 < N; ++i){
        int j = i + 1;
        int m1 = vec[i].size();
        int m2 = vec[j].size();
        for(int k = 0; k < m1; ++k){
            for(int l = 0; l < m2; ++l){
                int t1 = vec[i][k];
                int t2 = vec[j][l];
                int c = lca.get(t1, t2);
                if(3 == lca.depth[t1]+ lca.depth[t2] - 2*lca.depth[c]){
                    if(t1 > t2) swap(t1,t2);
                    p.insert({t1, t2});
                }
            }
        }
    }
    vector<int> ans(N);
    iota(ans.begin(), ans.end(), 1);
    int i1=1, i2=1, i3=1;
    for(auto it : p) {
        int i = it.first;
        int j = it.second;
        if( (ans[i] + ans[j])%3 == 0)continue;
        if( (ans[i] * ans[j])%3 == 0)continue;
        if((j-i)%3==0){
            if(ans[j]%3==1) swap(ans[j], ans[j-1]);
            else if(ans[j]%3==2 && j +1 < N) swap(ans[j], ans[j+1]);
            else if(ans[j]%3==2 && j +1 >= N) swap(ans[j], ans[j-2]);
        }
    }
    for(int i = 0; i < N; ++i){
        cout << ans[i];
        if(i==N-1) cout << endl;
        else cout << ' ';
    }
}