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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<stdio.h>
#include<stdlib.h>
int a[400005], b[400005];
int asc(const void* x, const void* y)
{
	if (a[*(int*)x] > a[*(int*)y])return 1;
	if (a[*(int*)x] < a[*(int*)y])return -1;
	return 0;
}
int main()
{
	int n;
	scanf("%d", &n);
	int i;
	for (i = 0; i < n - 1; i++)
	{
		i *= 2;
		scanf("%d %d", &a[i], &b[i]);
		a[i + 1] = b[i];
		b[i + 1] = a[i];
		i /= 2;
	}
	int c[400005];
	for (i = 0; i < 2 * n - 2; i++)
		c[i] = i;
	qsort(c, 2 * n - 2, sizeof(int), asc);
	int depth[200005];
	for (i = 0; i <= n; i++)
		depth[i] = -1;
	depth[1] = 0;
	int min, mid, max;
	int q[300005], qq;
	q[0] = 1;
	qq = 1;
	while (qq > 0)
	{
		qq--;
		i = q[qq];
		min = -1;
		max = 2 * n - 2;
		while (max - min > 1)
		{
			mid = (max + min) / 2;
			if (a[c[mid]] < i)
				min = mid;
			else
				max = mid;
		}
		if (max < 2 * n - 2)
		{
			while (a[c[max]] == i)
			{
				if (depth[b[c[max]]] < 0)
				{
					depth[b[c[max]]] = depth[i] + 1;
					q[qq] = b[c[max]];
					qq++;
				}
				max++;
				if (max == 2 * n - 2)
					break;
			}
		}
	}
	int l[3];
	l[0] = 3;
	l[1] = 1;
	l[2] = 2;
	int ans[200005];
	for (i = 1; i <= n; i++)
	{
		if (depth[i] % 2 > 0)
		{
			if (l[1] <= n)
			{
				ans[i] = l[1];
				l[1] += 3;
			}
			else
			{
				if (l[0] <= n)
				{
					ans[i] = l[0];
					l[0] += 3;
				}
				else
				{
					printf("-1\n");
					return 0;
				}
			}
		}
		else
		{
			if (l[2] <= n)
			{
				ans[i] = l[2];
				l[2] += 3;
			}
			else
			{
				if (l[0] <= n)
				{
					ans[i] = l[0];
					l[0] += 3;
				}
				else
				{
					printf("-1\n");
					return 0;
				}
			}
		}
	}
	for (i = 0; i < n - 1; i++)
		printf("%d ", ans[i + 1]);
	printf("%d\n", ans[n]);
	return 0;
}