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"
  "os"
  "strconv"
  "strings"
  "fmt"
)

var reader = bufio.NewReaderSize(os.Stdin, 1000000)
var writer = bufio.NewWriter(os.Stdout)
func NextLine() string {
  buffer := make([]byte, 0)
  for true {
    line, isPrefix, err := reader.ReadLine()
    if err != nil { panic(err) }
    buffer = append(buffer, line...)
    if !isPrefix { break }
  }
  return string(buffer)
}
func NextInt(A ...*int) {
  L := strings.Split(NextLine(), " ")
  for i, a := range A {
    *a, _ = strconv.Atoi(L[i])
  }
}
func NextIntVec(A *[]int) {
  L := strings.Split(NextLine(), " ")
  (*A) = make([]int, len(L))
  for i, l := range L {
    (*A)[i], _ = strconv.Atoi(l)
  }
}
func NextFloat(A ...*float64) {
  L := strings.Split(NextLine(), " ")
  for i, a := range A {
    *a, _ = strconv.ParseFloat(L[i], 64)
  }
}
func NextFloatVec(A *[]float64) {
  L := strings.Split(NextLine(), " ")
  (*A) = make([]float64, len(L))
  for i, l := range L {
    (*A)[i], _ = strconv.ParseFloat(l, 64)
  }
}
func Write(s interface{}) {
  fmt.Fprintln(writer, s)
}
func WriteIntVec(A []int) {
  S := make([]string, len(A))
  for i, a := range A {
    S[i] = strconv.Itoa(a)
  }
  Write(strings.Join(S, " "))
}
func Output() {
  _ = writer.Flush()
}

func MaxInt(A ...int) int {
  max := A[0]
  for _, a := range A {
    if max < a { max = a }
  }
  return max
}
func MinInt(A ...int) int {
  min := A[0]
  for _, a := range A {
    if a < min { min = a }
  }
  return min
}
func SumInt(A ...int) int {
  sum := 0
  for _, a := range A {
    sum += a
  }
  return sum
}
func AbsInt(x int) int {
  if x < 0 { x = -x }
  return x
}

func Mod(I []int, P int) int {
  N := 0
  for _, i := range I {
    N = N * 10 + i
    N %= P
  }
  return N
}

func Solve() {
  var N int
  NextInt(&N)
  T := make([][]int, N)
  for i := range T {
    T[i] = make([]int, 0)
  }
  var a, b int
  for i := 1; i < N; i++ {
    NextInt(&a, &b)
    a, b = a - 1, b - 1
    T[a] = append(T[a], b)
    T[b] = append(T[b], a)
  }
  D := make([]int, N)
  D[0] = 1
  pos := 1
  Q := make([]int, N)
  for _, q := range Q {
    for _, adj := range T[q] {
      if 0 < D[adj] { continue }
      D[adj] = D[q] + 1
      Q[pos] = adj
      pos++
    }
  }
  for i := range D {
    D[i] = -1
  }
  Q[0] = Q[N - 1]
  D[Q[0]] = 1
  pos = 1
  for _, q := range Q {
    for _, adj := range T[q] {
      if 0 < D[adj] { continue }
      D[adj] = D[q] + 1
      Q[pos] = adj
      pos++
    }
  }
  k1, k2, k3 := 0, 0, 0
  A := make([]int, N)
  if D[Q[N - 1]] < 4 {
    for i := range A { A[i] = i + 1 }
    WriteIntVec(A)
    return
  }
  for i := N - 1; 0 <= i; i-- {
    q := Q[i]
    d := D[q]
    if d % 2 == 0 {
      if 3 * k1 + 1 <= N {
        A[q] = 3 * k1 + 1
        k1++
      } else if 3 * k3 + 3 <= N {
        A[q] = 3 * k3 + 3
        k3++
      }
    } else {
      if 3 * k2 + 2 <= N {
        A[q] = 3 * k2 + 2
        k2++
      } else if 3 * k3 + 3 <= N {
        A[q] = 3 * k3 + 3
        k3++
      }
    }
  }
  if MinInt(A...) == 0 {
    Write(-1)
    return
  }
  WriteIntVec(A)
}

func main() {
  Solve()
  Output()
}