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)

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vvint = vector<vint>;
using vvvint = vector<vvint>;
using vdouble = vector<double>;
using vvdouble = vector<vdouble>;
using vvvdouble = vector<vvdouble>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using uint = unsigned int;
using ull = unsigned long long;

template<typename T> using uset = unordered_set<T>;
template<typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\n';
constexpr int mod = 1000000007;

template<typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template<typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) { return (a > b && (a = b, true)); }
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) { return (a < b && (a = b, true)); }

template<typename T>
ostream& operator <<(ostream &os, vector<T> &v) {
	os << v[0];
	for (int i = 1; i < v.size(); i++) os << " " << v[i];
	return (os);
}

template<typename T>
istream& operator >>(istream &is, vector<T> &v) {
	for (auto &u : v) is >> u; return (is);
}

template<typename T1, typename T2>
istream& operator >>(istream &is, pair<T1, T2> &p) {
	return (is >> p.first >> p.second);
}

vint color;
vvint G;
int N;
vint ccnt(2);
vint ans;
void dfs(int u, int c, int prev) {
	color[u] = c;
	ccnt[c]++;
	for (auto &v : G[u]) {
		if (prev != v) dfs(v, 1-c, u);
	}
}

void Main() {
	cin >> N;
	color = vint(N);
	G = vvint(N);
	ans = vint(N, 0);
	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);
	}
	dfs(0, 0, -1);
	if (ccnt[0] <= N/3) {
		int cnt = 0;
		vector<bool> flag(N+1, false);
		for (int i = 0; i < N; i++) {
			if (color[i] == 0) {
				ans[i] = cnt * 3 + 3;
				flag[cnt * 3 + 3] = true;
				cnt++;
			}
		}
		int val = 1;
		for (int i = 0; i < N; i++) {
			if (color[i] == 1) {
				while (flag[val]) val++;
				ans[i] = val;
				flag[val] = true;
			}
		}
		cout << ans << endl;
	} else if (ccnt[1] <= N/3) {
		int cnt = 0;
		vector<bool> flag(N+1, false);
		for (int i = 0; i < N; i++) {
			if (color[i] == 1) {
				ans[i] = cnt * 3 + 3;
				flag[cnt * 3 + 3] = true;
				cnt++;
			}
		}
		int val = 1;
		for (int i = 0; i < N; i++) {
			if (color[i] == 0) {
				while (flag[val]) val++;
				ans[i] = val;
				flag[val] = true;
			}
		}
		cout << ans << endl;
	} else {
		int size1 = (N+2) / 3, size2 = (N+1) / 3;
		int cnt1 = 0, cnt2 = 0;
		vector<bool> flag(N+1, false);
		for (int i = 0; i < N; i++) {
			if (color[i] == 0 && cnt1 < size1) {
				ans[i] = cnt1*3 + 1;
				flag[cnt1*3+1] = true;
				cnt1++;
			}
			if (color[i] == 1 && cnt2 < size2) {
				ans[i] = cnt2*3 + 2;
				flag[cnt2*3+2] = true;
				cnt2++;
			}
		}
		int val = 1;
		for (int i = 0; i < N; i++) {
			if (ans[i] == 0) {
				while (flag[val]) val++;
				ans[i] = val;
				flag[val] = true;
			}
		}
		cout << ans << endl;
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout << fixed << setprecision(20);
	Main();
	return (0);
}