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 Rust (1.15.1) (judge verdict: Accepted)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use competitive::prelude::*;

fn main() {
    input! {
        n: usize,
        ab: [(Usize1, Usize1); n-1],
    }

    let mut g = vec![vec![]; n];
    for (u, v) in ab {
        g[u].push(v);
        g[v].push(u);
    }

    let mut dmap = vec![0; n];

    dfs(0, None, 0, &g, &mut dmap);

    let mut even = 0;

    for i in 0..n {
        if dmap[i] % 2 == 0 {
            even += 1;
        }
    }

    let odd = n - even;

    let mut alloc = vec![0; n];

    let mut qs = vec![vec![]; 3];
    for i in 1..=n {
        qs[i % 3].push(i);
    }

    if odd * 3 > n && even * 3 > n {
        for i in 0..n {
            alloc[i] = if dmap[i] % 2 == 0 {
                qs[1].pop().unwrap_or_else(|| qs[0].pop().unwrap())
            } else {
                qs[2].pop().unwrap_or_else(|| qs[0].pop().unwrap())
            }
        }
    } else if even * 3 <= n {
        for i in 0..n {
            alloc[i] = if dmap[i] % 2 == 0 {
                qs[0].pop().unwrap()
            } else {
                qs[1]
                    .pop()
                    .unwrap_or_else(|| qs[2].pop().unwrap_or_else(|| qs[0].pop().unwrap()))
            }
        }
    } else {
        for i in 0..n {
            alloc[i] = if dmap[i] % 2 == 1 {
                qs[0].pop().unwrap()
            } else {
                qs[1]
                    .pop()
                    .unwrap_or_else(|| qs[2].pop().unwrap_or_else(|| qs[0].pop().unwrap()))
            }
        }
    }

    echo!(alloc);
}

fn dfs(cur: usize, prev: Option<usize>, dep: usize, g: &Vec<Vec<usize>>, dmap: &mut Vec<usize>) {
    dmap[cur] = dep;

    for &next in g[cur].iter() {
        if Some(next) == prev {
            continue;
        }

        dfs(next, Some(cur), dep + 1, g, dmap);
    }
}
*/

fn main() {
    let exe = "/tmp/bin67D19276";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";