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 Go (1.6) (judge verdict: Runtime Error)

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var sc = bufio.NewScanner(os.Stdin)

func stringLineScan() string {
	sc.Scan()
	return sc.Text()
}

func main() {
	buf := make([]byte, 0)
	sc.Buffer(buf, 1000000009)
	sc.Split(bufio.ScanWords)
	n, _ := strconv.Atoi(stringLineScan())
	tree := make([][]int, n)
	for i := 0; i < n; i++ {
		t := make([]int, 0)
		tree[i] = t
	}
	for i := 0; i < n-1; i++ {
		a, _ := strconv.Atoi(stringLineScan())
		b, _ := strconv.Atoi(stringLineScan())
		a--
		b--
		tree[a] = append(tree[a], b)
		tree[b] = append(tree[b], a)
	}
	three := make([][]int, n)
	for i := 0; i < n; i++ {
		t := make([]int, 0)
		three[i] = t
	}
	for i := 0; i < n; i++ {
		searched := make([]bool, n)
		match := make([]int, 0)
		searched, match = dfs(i, 0, tree, searched, match)
		three[i] = match
	}
	m1 := make([]int, 0)
	m2 := make([]int, 0)
	m3 := make([]int, 0)
	f1, f2, f3, can := true, true, true, true
	for i := 1; i <= n; i += 3 {
		m1 = append(m1, i)
	}
	for i := 2; i <= n; i += 3 {
		m2 = append(m2, i)
	}
	for i := 3; i <= n; i += 3 {
		m3 = append(m3, i)
	}
	l1, l2, l3 := len(m1), len(m2), len(m3)
	i1, i2, i3 := 0, 0, 0
	ans := make([]int, n)
	ans[0] = m1[i1]
	i1++
	count := 1
	for i, list := range three {
		if ans[i] == 0 {
			if f1 {
				ans[i] = m1[i1]
				i1++
				count++
				if i1 == l1 {
					f1 = false
				}
			} else if f2 {
				ans[i] = m2[i2]
				i2++
				count++
				if i2 == l2 {
					f2 = false
				}
			} else {
				ans[i] = m3[i3]
				i3++
				count++
				if i3 == l3 {
					f3 = false
				}
			}
		}
		z := ans[i] % 3
		for _, v := range list {
			if ans[v] == 0 {
				m := 0
				if f1 && z == 0 {
					m = 1
				} else if f2 && z == 0 {
					m = 2
				} else if f1 && z == 2 {
					m = 1
				} else if f2 && z == 1 {
					m = 2
				} else if f3 {
					m = 3
				}
				if m == 0 {
					can = false
					break
				}
				if m == 1 {
					ans[v] = m1[i1]
					i1++
					count++
					if i1 == l1 {
						f1 = false
					}
				} else if m == 2 {
					ans[v] = m2[i2]
					i2++
					count++
					if i2 == l2 {
						f2 = false
					}
				} else {
					ans[v] = m3[i3]
					i3++
					count++
					if i3 == l3 {
						f3 = false
					}
				}
			} else {
				if (z + ans[v])%3 != 0{
					can = false
					break
				}
			}
		}
		if count == n {
			break
		}
		if !can {
			break
		}
	}
	for i := 0; i < n; i++ {
		if ans[i] == 0 {
			if f1 {
				ans[i] = m1[i1]
				i1++
				count++
				if i1 == l1 {
					f1 = false
				}
			} else if f2 {
				ans[i] = m2[i2]
				i2++
				count++
				if i2 == l2 {
					f2 = false
				}
			} else {
				ans[i] = m3[i3]
				i3++
				count++
				if i3 == l3 {
					f3 = false
				}
			}
		}
	}
	if can {
		fmt.Println(strings.Trim(fmt.Sprint(ans), "[]"))
	} else {
		fmt.Println(-1)
	}
}
func dfs(init int, depth int, tree [][]int, searched []bool, match []int) ([]bool, []int) {
	depth++
	searched[init] = true
	for _, i := range tree[init] {
		if depth == 3 && searched[i] == false {
			match = append(match, i)
			searched[i] = true
		} else if searched[i] == false {
			searched[i] = true
			searched, match = dfs(i, depth, tree, searched, match)
		}
	}
	return searched, match
}