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 <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>
#include <iomanip>
#include <random>


#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout << fixed << setprecision(20);

	int n; cin >> n;
	unordered_multimap<int, int> g;
	FOR_LT(i, 0, n - 1) {
		int a, b; cin >> a >> b;
		g.insert({ a, b });
		g.insert({b, a
	});
	}

	struct P {
		int i;
		bool b;
		bool s;
	};
	vector<P> ps(n + 1);

	ps[1].b = true;
	ps[1].s = true;
	queue<int> search;
	search.push(1);
	int nb = 1;
	int nc = 0;
	while (!search.empty()) {
		int c = search.front();
		search.pop();

		auto it = g.find(c);
		while (it != g.end() && it->first == c) {
			int b = it->second;
			if (ps[b].s) {
			}
			else {
				ps[b].b = !ps[c].b;
				if (ps[b].b) nb++;
				else nc++;
				ps[b].s = true;
				search.push(b);
			}
			it++;
		}
	}
  
	int r1 = 1;
	int r2 = 2;
	int r3 = 3;

	if (nb <= (n / 3)) {
		FOR_LE(i, 1, n) {
			if (ps[i].b) {
				ps[i].i = r1;
				r1 += 3;
			}
			if (r1 <= n) {
				ps[i].i = r1;
				r1 += 3;
			}
			else if (r2 <= n) {
				ps[i].i = r2;
				r2 += 3;
			}
			else {
				ps[i].i = r3;
				r3 += 3;
			}
		}
		return 0;
	}
	if (nc <= (n / 3)) {
		FOR_LE(i, 1, n) {
			if (!ps[i].b) {
				ps[i].i = r1;
				r1 += 3;
			}
			if (r1 <= n) {
				ps[i].i = r1;
				r1 += 3;
			}
			else if (r2 <= n) {
				ps[i].i = r2;
				r2 += 3;
			}
			else {
				ps[i].i = r3;
				r3 += 3;
			}
		}
		return 0;
	}

	FOR_LE(i, 1, n) {
		if (ps[i].b) {
			if (r1 <= n) {
				ps[i].i = r1;
				r1 += 3;
			}
			else if (r3 <= n) {
				ps[i].i = r3;
				r3 += 3;
			}
			else {
				ps[i].i = r2;
				r2 += 3;
			}
		}
		else {
			if (r2 <= n) {
				ps[i].i = r2;				
				r2 += 3;
			}
			else if (r3 <= n) {
				ps[i].i = r3;
				r3 += 3;
			}
			else {
				ps[i].i = r1;
				r1 += 3;
			}
		}
	}

	FOR_LE(i, 1, n) {
		cout << ps[i].i << " ";
	}
	cout << endl;

	return 0;
}