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: Runtime Error)

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <string>
#include <iomanip>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <complex>
#include <ctime>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef int itn;
const ll LINF = 1e18;
const int INF = 1e9;

//マクロ定義
#define vvint(vec,n,m,l) vector<vector<int>> vec(n, vector<int>(m,l));	// lで初期化
#define vvll(vec,n,m,l) vector<vector<ll>> vec(n,vector<ll>(m,l));
#define vint vector<int>
#define pint pair<int,int>
#define rep(i,a) for(int i=0;i<(a);i++)
#define all(x) (x).begin(),(x).end()
#define debug system("pause")				//デバッグ用
#define ret return 0

template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

using Graph = vector<vector<ll>>;

#define ketasitei setprecision(15) //15桁表示

const ll MOD = 1000000007;
const double PI = 3.1415926535897932;


int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	ll n;
	cin >> n;
	vector<ll> a(n-1), b(n-1);

	Graph g(n);

	rep(i, n-1)
	{
		cin >> a[i] >> b[i];
		g[a[i]-1].push_back(b[i]-1);
		g[b[i]-1].push_back(a[i]-1);
	}

	queue<pair<ll,ll>> dfs;
	rep(i, g[0].size())
	{
		dfs.emplace(g[0][i],1);
	}

	vector<ll> red, blue;
	red.push_back(0);
	vector<ll> watched(n, 0);
	watched[0] = 1;

	while (!dfs.empty())
	{
		pair<ll,ll> p = dfs.front();
		dfs.pop();

		if (watched[p.first])
			continue;

		if (p.second % 2)
		{
			blue.push_back(p.first);
		}
		else
		{
			red.push_back(p.first);
		}

		watched[p.first] = 1;
		rep(i, g[p.first].size())
		{
			dfs.emplace(g[p.first][i], p.second+1);
		}

	}

	ll zero = n / 3;

	ll rs = red.size(), bs = blue.size();

	vector<ll> ans(n);

	ll one = n / 3 + (n % 3 > 0);
	ll two = n / 3 + (n % 3 == 2);
	ll o = 0, t = 0;

	if (rs<=n/3)
	{
		for (int i = 0; i < zero; i++)
			ans[red[i]] = i * 3 + 3;

		for (int i = 0; i < n - zero; i++)
		{
			if (one)
			{
				ans[blue[i]] = o * 3 + 1;
				o++;
				one--;
			}
			else
			{
				ans[blue[i]] = t * 3 + 2;
				t++;
			}
		}
	}
	else if (bs <= n / 3)
	{
		for (int i = 0; i < zero; i++)
			ans[blue[i]] = i * 3 + 3;

		for (int i = 0; i < n - zero; i++)
		{
			if (one)
			{
				ans[red[i]] = o * 3 + 1;
				o++;
				one--;
			}
			else
			{
				ans[red[i]] = t * 3 + 2;
				t++;
			}
		}
	}
	else
	{
		ll rnow = 0, bnow = 0;
		for (int i = 0; i < n / 3 + (n % 3 > 0); i++)
		{
			if (rs < bs)
			{
				ans[blue[bnow]] = bnow *3 + 1;
				bnow++;
			}
			else
			{
				ans[red[rnow]] = rnow * 3 + 1;
				rnow++;
			}
		}
		for (int i = 0; i < n / 3 + (n % 3 == 2); i++)
		{
			if (rs < bs)
			{
				ans[red[rnow]] = rnow * 3 + 2;
				rnow++;
			}
			else
			{
				ans[blue[bnow]] = bnow * 3 + 2;
				bnow++;
			}
		}
		
		ll rrnow = rnow, bbnow = bnow;
		ll zeronow = 0;
		for (int i=0;i+rrnow<red.size();i++)
		{
			ans[red[rnow]] = zeronow * 3 + 3;
			rnow++;
			zeronow++;
		}
		for (int i = 0; i + bbnow < blue.size(); i++)
		{
			ans[blue[bnow]] = zeronow * 3 + 3;
			bnow++;
			zeronow++;
		}

	}

	cout << ans[0];

	rep(i, n-1)
	{
		cout << " " << ans[i+1];
	}
	cout << endl;

	return 0;
}