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: Accepted)

package main

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

func out(x ...interface{}) {
	//	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

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

type node struct {
	to []int
}

var g0 []int
var g1 []int
var used []int

func dfs(cnt, i, N int, n []node) {
	used[i] = 1
	if cnt%2 == 0 {
		g0 = append(g0, i)
	} else {
		g1 = append(g1, i)
	}
	for _, v := range n[i].to {
		if used[v] == 1 {
			continue
		}
		dfs(cnt+1, v, N, n)
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	N := getInt()
	n := make([]node, N)
	used = make([]int, N)
	for i := 0; i < N-1; i++ {
		f, t := getInt()-1, getInt()-1
		n[f].to = append(n[f].to, t)
		n[t].to = append(n[t].to, f)
	}

	dfs(0, 0, N, n)

	out(g0, g1)
	p := make([]int, 0, N)
	cnt0 := 0
	for i := 1; i <= N; i++ {
		if i%3 == 1 {
			p = append(p, i)
			cnt0++
		}
	}
	start1 := cnt0
	cnt1 := 0
	for i := 1; i <= N; i++ {
		if i%3 == 2 {
			p = append(p, i)
			cnt1++
		}
	}
	start2 := start1 + cnt1
	cnt3 := 0
	for i := 1; i <= N; i++ {
		if i%3 == 0 {
			p = append(p, i)
			cnt3++
		}
	}
	out("pass", len(p))

	x := make([]int, N)
	if len(g0) <= cnt3 {
		out("pat0")
		for i := 0; i < len(g0); i++ {
			x[g0[i]] = p[len(p)-1-i]
		}
		for i := 0; i < len(g1); i++ {
			x[g1[i]] = p[i]
		}
	} else if len(g1) <= cnt3 {
		out("pat1")
		for i := 0; i < len(g1); i++ {
			x[g1[i]] = p[len(p)-1-i]
		}
		for i := 0; i < len(g0); i++ {
			x[g0[i]] = p[i]
		}
	} else {
		out("pat2")
		idx := 0
		for i := 0; i < cnt0; i++ {
			x[g0[idx]] = p[i]
			idx++
		}
		out("--pass")
		for i, j := idx, 0; i < len(g0); i++ {
			out(start2 + j)
			x[g0[i]] = p[start2+j]
			j++
		}
		idx = 0
		out("--pass")
		for i := 0; i < cnt1; i++ {
			x[g1[idx]] = p[start1+i]
			idx++
		}
		out("--pass")
		for i, j := idx, len(p)-1; i < len(g1); i++ {
			out(start2+j, start1, cnt0, cnt1, cnt3)
			x[g1[i]] = p[j]
			j--
		}
		out("--pass")

	}

	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	for i := 0; i < N; i++ {
		fmt.Fprintf(w, "%d ", x[i])
	}
	fmt.Fprintf(w, "\n")
}