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 (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <iostream>
#include <cmath>
#include <numeric>
#include <algorithm>
#include <deque>
#include <vector>
#include <unordered_map>
#include <iomanip>
#include <map>
#include <stack>
#include <queue>
#include <functional>
#include <climits>



int main() {
	int n; std::cin >> n;
	std::vector<std::vector<int>> tree(n);
	for (auto i = 1; i < n; ++i) {
		int a, b; std::cin >> a >> b; --a; --b;
		tree[a].push_back(b);
		tree[b].push_back(a);
	}
	std::vector<int> prev(n, -1); prev[0] = 0;
	std::vector<int> parity(n, -1); parity[0] = 1;
	std::stack<int> stack; stack.push(0);
	while (!stack.empty()) {
		const auto top = stack.top(); stack.pop();
		for (const auto next : tree[top]) if (parity[next] == -1) {
			prev[next] = top;
			parity[next] = 3 - parity[top];
			stack.push(next);
		}
	}
	const int one_count = std::count(parity.begin(), parity.end(), 1);
	const int two_count = n - one_count;
	std::vector<int> result;
	int one_rest = (n + 2) / 3;
	int two_rest = (n + 1) / 3;
	int zero_rest = n / 3;
	if (one_count >= n / 3 && two_count >= n / 3) {
		if (one_count < two_count) {
			for (auto& p : parity) p = 3 - p;
		}
		for (auto i = 0; i < n; ++i) {
			if (parity[i] == 1) {
				if (one_rest > 0) {
					result.push_back(one_rest * 3 - 2);
					--one_rest;
				}
				else {
					result.push_back(zero_rest * 3);
					--zero_rest;
				}
			}
			else {
				if (two_rest > 0) {
					result.push_back(two_rest * 3 - 1);
					--two_rest;
				}
				else  {
					result.push_back(zero_rest * 3);
					--zero_rest;
				}
			}
		}
	}
	else if (one_count < two_count) {
		for (const auto p : parity) {
			if (p == 1) {
				result.push_back(zero_rest * 3);
				--zero_rest;
			}
			else {
				if (one_rest > 0) {
					result.push_back(one_rest * 3 - 2);
					--one_rest;
				}
				else if (two_rest > 0) {
					result.push_back(two_rest * 3 - 1);
					--two_rest;
				}
				else {
					result.push_back(zero_rest * 3);
					--zero_rest;
				}
			}
		}
	}
	else {
		for (const auto p : parity) {
			if (p == 2) {
				result.push_back(zero_rest * 3);
				--zero_rest;
			}
			else {
				if (one_rest > 0) {
					result.push_back(one_rest * 3 - 2);
					--one_rest;
				}
				else if (two_rest > 0) {
					result.push_back(two_rest * 3 - 1);
					--two_rest;
				}
				else {
					result.push_back(zero_rest * 3);
					--zero_rest;
				}
			}
		}
	}
	for (auto i = 0; i < n; ++i) {
		if (i != 0) std::cout << ' ';
		std::cout << result[i];
	}
	std::cout << '\n';
}