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

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use proconio::input;
#[allow(unused_imports)]
use proconio::marker::*;
#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
#[allow(unused_imports)]
use std::f64::consts::*;

#[allow(unused)]
const INF: usize = std::usize::MAX / 4;
#[allow(unused)]
const M: usize = 1000000007;

fn first(set: &HashSet<usize>) -> Option<usize> {
    set.iter().nth(0).map(|&x| x)
}

fn main() {
    input! {
        n: usize,
        ab: [(Usize1, Usize1); n - 1],
    }
    let mut graph = vec![vec![]; n];
    for &(ai, bi) in &ab {
        graph[ai].push(bi);
        graph[bi].push(ai);
    }
    let mut three_neighbors = vec![HashSet::new(); n];
    for u in 0..n {
        for &v1 in &graph[u] {
            for &v2 in &graph[v1] {
                if u == v2 {
                    continue;
                }
                for &v3 in &graph[v2] {
                    if v3 != u && v3 != v1 {
                        three_neighbors[u].insert(v3);
                    }
                }
            }
        }
    }
    // for i in 0..n {
    //     eprintln!("{:?}", three_neighbors[i]);
    // }
    // let tmp = three_neighbors.clone();

    let mut indices = (0..n).collect::<Vec<usize>>();
    indices.sort_by_key(|&i| three_neighbors[i].len());
    indices.reverse();

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

    let mut zero = (3..=n).step_by(3).collect::<HashSet<usize>>();
    let mut one = (1..=n).step_by(3).collect::<HashSet<usize>>();
    let mut two = (2..=n).step_by(3).collect::<HashSet<usize>>();

    for &i in &indices {
        if three_neighbors[i].len() == 0 {
            continue;
        }

        let js = three_neighbors[i]
            .iter()
            .map(|&j| j)
            .collect::<Vec<usize>>();

        if p[i] != 0 {
            if p[i] % 3 == 1 {
                if two.len() < three_neighbors[i].len() {
                    println!("-1");
                    return;
                }
                if let Some(x) = first(&one) {
                    p[i] = x;
                    for &j in &js {
                        if let Some(y) = first(&two) {
                            p[j] = y;
                            three_neighbors[j].remove(&i);
                            two.remove(&y);
                        }
                    }
                }
            } else {
                if one.len() < three_neighbors[i].len() {
                    println!("-1");
                    return;
                }
                if let Some(x) = first(&two) {
                    p[i] = x;
                    for &j in &js {
                        if let Some(y) = first(&one) {
                            p[j] = y;
                            three_neighbors[j].remove(&i);
                            one.remove(&y);
                        }
                    }
                }
            }
            continue;
        }

        if let Some(x) = first(&zero) {
            p[i] = x;
            let js = three_neighbors[i]
                .iter()
                .map(|&j| j)
                .collect::<Vec<usize>>();
            for &j in &js {
                three_neighbors[j].remove(&i);
            }
            zero.remove(&x);
        } else {
            if one.is_empty() || two.is_empty() {
                println!("-1");
                return;
            }
            if one.len() > two.len() {
                if one.len() < three_neighbors[i].len() {
                    println!("-1");
                    return;
                }
                if let Some(x) = first(&two) {
                    p[i] = x;
                    for &j in &js {
                        if let Some(y) = first(&one) {
                            p[j] = y;
                            three_neighbors[j].remove(&i);
                            one.remove(&y);
                        }
                    }
                    two.remove(&x);
                }
            } else {
                if two.len() < three_neighbors[i].len() {
                    println!("-1");
                    return;
                }
                if let Some(x) = first(&one) {
                    p[i] = x;
                    for &j in &js {
                        if let Some(y) = first(&two) {
                            p[j] = y;
                            three_neighbors[j].remove(&i);
                            two.remove(&y);
                        }
                    }
                    one.remove(&x);
                }
            }
        }
    }

    for i in 0..n {
        if p[i] == 0 {
            if let Some(x) = first(&zero) {
                p[i] = x;
                zero.remove(&x);
            } else if let Some(x) = first(&one) {
                p[i] = x;
                one.remove(&x);
            } else if let Some(x) = first(&two) {
                p[i] = x;
                two.remove(&x);
            }
        }
    }

    // for i in 0..n {
    //     for &j in &tmp[i] {
    //         if (p[i] + p[j]) % 3 != 0 && (p[i] * p[j]) % 3 != 0 {
    //             eprintln!("error");
    //         }
    //     }
    // }

    for &pi in &p {
        println!("{}", pi);
    }
}
*/

fn main() {
    let exe = "/tmp/bin03AEDD39";
    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>";