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)

#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/stack:512000000")
#include<utility>
#include<iostream>
#include<fstream>
#include<vector>
#include<array>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<functional>
#include<random>
#include<climits>
#include<queue>
#include<stack>
#include<deque>
#include<bitset>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<cassert>
#include<ccomplex>

//4Head

#define all(x) x.begin(), x.end()
#define mp make_pair
#define X first
#define Y second

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
mt19937_64 gen(time(NULL));

ll const mod = 1e9 + 7;

namespace {
	ll mul(ll a, ll b) {
		ll val = a * b - (ll)((ld)a * b / mod) * mod;
		if (val < 0) val += mod;
		if (val >= mod) val -= mod;
		return val;
	}

	ll poww(ll a, ll b) {
		ll val = 1;
		a %= mod;
		while (b > 0) {
			if (b % 2) val = mul(a, val);
			a = mul(a, a);
			b >>= 1;
		}
		return val % mod;
	}

	ll inv(ll a) {
		return poww(a, mod - 2);
	}
}

ll const maxn = 3e5 + 5;

vector<ll>g[maxn];
bool used[maxn];
ll d[maxn];


signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	//freopen("mountains.in", "r", stdin);
	//freopen("mountains.out", "w", stdout);
	ll n; cin >> n;
	for (ll i = 0; i < n - 1; i++) {
		ll u, v; cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	queue<ll>q;
	q.push(1); used[1] = 0;
	while (!q.empty()) {
		ll u = q.front();
		q.pop();
		for (ll v : g[u]) {
			if (used[v]) continue;
			d[v] = d[u] + 1;
			used[v] = 1;
			q.push(v);
		}
	}

	vector<ll>ans(n + 1);
	vector<ll>cnt(2);
	ll lst1 = 1, lst2 = 2, lst0 = 3;
	for (ll i = 1; i <= n; i++) {
		cnt[d[i] % 2]++;
	}
	if (cnt[0] <= n / 3) {
		for (ll i = 1; i <= n; i++) {
			if (d[i] % 2 == 0) {
				ans[i] = lst0; lst0 += 3;
			} else {
				if (lst1 <= n) {
					ans[i] = lst1; lst1 += 3;
				} else if (lst2 <= n) {
					ans[i] = lst2; lst2 += 3;
				} else {
					ans[i] = lst0; lst0 += 3;
				}
			}
		}
	} else if (cnt[1] <= n/3) {
		for (ll i = 1; i <= n; i++) {
			if (d[i] % 2 == 1) {
				ans[i] = lst0; lst0 += 3;
			} else {
				if (lst1 <= n) {
					ans[i] = lst1; lst1 += 3;
				} else if (lst2 <= n) {
					ans[i] = lst2; lst2 += 3;
				} else {
					ans[i] = lst0; lst0 += 3;
				}
			}
		}
	} else {
		for (ll i = 1; i <= n; i++) {
			if (d[i] % 2 + 1 == 1) {
				if (lst1 <= n) {
					ans[i] = lst1;
					lst1 += 3;
				} else {
					ans[i] = lst0;
					lst0 += 3;
				}
			} else {
				if (lst2 <= n) {
					ans[i] = lst2;
					lst2 += 3;
				} else {
					ans[i] = lst0;
					lst0 += 3;
				}
			}
		}
	}




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

/*


*/