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: Time Limit Exceeded)

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <iostream>
#include <string>

#include <algorithm>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define PB push_back
#define MP make_pair

#define FOR(i, b, e) for(int i=(b);i<(e);i++)
#define REP(i, n) FOR(i,0,n)

#define ALL(v) (v).begin(),(v).end()

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;

typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<PII> VPII;

const int MOD = 1000000007;
const int INF = 1e9;

const double EPS = 1e-9;
const double PI = acos(-1.0);

const int DX[4] = {1, 0, -1, 0};
const int DY[4] = {0, 1, 0, -1};

LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }
LL lcm(LL a, LL b) { return a / gcd(a, b) * b; }

typedef struct {
    VI n;
} node;

int N;
vector<node> T;

int main() {
    cin >> N;
    T = vector<node>(N);
    REP (i, N - 1) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        T[a].n.PB(b);
        T[b].n.PB(a);
    }
    VPII vpii;
    set<PII> spii;
    VI S;
    S.PB(0);
    vector<bool> V(N, false);
    while (!S.empty()) {
        VPII s;
        vector<bool> v(N, false);
        int i = S.back();
        S.pop_back();
        if (V[i]) {
            continue;
        }
        V[i] = true;
        REP (j, T[i].n.size()) {
            S.PB(T[i].n[j]);
        }
        s.PB(MP(i, 0));
        while (!s.empty()) {
            PII p = s.back();
            s.pop_back();
            if (v[p.first]) {
                continue;
            }
            v[p.first] = true;
            if (p.second == 3) {
                //vpii.PB(MP(i, p.first));
                if (i < p.first) {
                    spii.insert(MP(i, p.first));
                }
                else {
                    spii.insert(MP(p.first, i));
                }
            }
            else {
                REP (j, T[p.first].n.size()) {
                    s.PB(MP(T[p.first].n[j], p.second + 1));
                }
            }
        }
    }
    VI ans;
    REP (i, N) {
        ans.PB(i + 1);
    }
    do {
        bool f = true;
        for (auto itr = spii.begin(); itr != spii.end(); itr++) {
            int t1 = (*itr).first;
            int t2 = (*itr).second;
            if ((ans[t1] + ans[t2]) % 3 == 0 || (ans[t1] * ans[t2]) % 3 == 0) {
                continue;
            }
            f = false;
            break;
        }
        if (f) {
            REP (i, N - 1) {
                cout << ans[i] << ' ';
            }
            cout << ans[N - 1] << endl;
            return 0;
        }
    }
    while (next_permutation(ALL(ans)));
    cout << -1 << endl;
    return 0;
}