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: Wrong Answer)

package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 3
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	ee := make([][]Edge, n)
	for i := 0; i < n-1; i++ {
		a := getNextInt(scanner) - 1
		b := getNextInt(scanner) - 1
		ee[a] = append(ee[a], Edge{
			to: b,
			id: i,
		})
		ee[b] = append(ee[b], Edge{
			to: a,
			id: i,
		})
	}

	tree := make([]int, n)
	dfs(0, -1, 0, tree, ee)

	cnt := map[int]int{}
	for i := 0; i < n; i++ {
		cnt[tree[i]] = cnt[tree[i]] + 1
	}
	for i := 0; i < n; i++ {
		if cnt[1] < cnt[2] {
			tree[i] = 3 - tree[i]
		}
	}

	cc := [3]int{
		1,
		0,
		0,
	}
	lm := [3]int{
		n / 3,
		n / 3,
		n / 3,
	}
	if n%3 != 0 {
		lm[1]++
	}
	if n%3 == 2 {
		lm[2]++
	}
	ans := make([]int, n)
	for i := 0; i < n; i++ {
		if cc[tree[i]] >= lm[tree[i]] {
			tree[i] = 0
		}
		ans[i] = cc[tree[i]]*3 + tree[i]
		cc[tree[i]]++
	}
	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, ans[i])
	}
}

// Edge ...
type Edge struct {
	to, id int
}

func dfs(i, p, c int, tree []int, ee [][]Edge) {
	tree[i] = (c % 2) + 1
	for _, e := range ee[i] {
		if e.to == p {
			continue
		}
		dfs(e.to, i, c+1, tree, ee)
	}
}