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: Runtime Error)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use std::str::FromStr;
use std::fmt::Debug;
use std::cmp;
#[allow(dead_code)]
fn read_as_vec<T>() -> Vec<T>
where T: FromStr, <T as FromStr>::Err : Debug{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().split_whitespace().map(|c| T::from_str(c).unwrap()).collect()

}

#[allow(dead_code)]
fn read_as_string() -> String{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

fn main(){
    let n = read_as_vec::<usize>()[0];
    let mut tree: Vec<Vec<usize>> = vec![vec![];n+1];

    for _ in 0..n-1{
        let line = read_as_vec::<usize>();
        let (a, b) = (line[0], line[1]);
        tree[a].push(b);
    }

    let mut v: Vec<(usize, usize, usize, usize)> = vec![];
    for i in 1..n+1{
        for j in 0..tree[i].len(){
            if tree[i].len() -1 >0 && tree[tree[i][j]].len() > 0{

                v.push(
                    (i, tree[i][j], tree[i].len() - 1, tree[tree[i][j]].len())
                );
            }
        }
    }

    if v.len() == 0{
        for i in 1..n+1{
            print!("{} ", i);
        }
        println!("");
        return;
    }

    v.sort_by_key(|x| cmp::max(x.2, x.3));
    v.reverse();

    let mut used = vec![false;n+1];
    used[0] = true;
    let mut ans = vec![0;n+1];

    let mut rest_3mul: Vec<usize> = vec![];

    for i in 1..n+1{
        if i*3 <= n{
            rest_3mul.push(i*3);
        }else{
            break;
        }
    }
    let mut  rest_1mod: Vec<usize> = vec![];
    for i in 1..n+1{
        if i%3 == 1{
            rest_1mod.push(i);
        }
    }
    let mut  rest_2mod: Vec<usize> = vec![];
    for i in 1..n+1{
        if i%3 == 2{
            rest_1mod.push(i);
        }
    }

    for (x, y, xv, yv) in &v{
        let lesser_nodes: &Vec<usize>;
        let bigger_nodes: &Vec<usize>;
        if *xv > *yv{
            lesser_nodes = &tree[*y];
            bigger_nodes = &tree[*x];
        }else{
            lesser_nodes = &tree[*x];
            bigger_nodes = &tree[*y];
        }
        for node in lesser_nodes{
            if *node == *y || *node == *x{
                continue;
            }
            if rest_3mul.len() > 0{
                let used_value = rest_3mul.pop().unwrap();
                used[used_value] = true;
                ans[*node] = used_value;
            }else if rest_1mod.len() > 0 && rest_2mod.len() > 0{
                if rest_1mod.len() > rest_2mod.len(){
                    let val_2mod = rest_2mod.pop().unwrap();
                    used[val_2mod] = true;
                    ans[*node] = val_2mod;
                    for big_node in bigger_nodes{
                        if rest_1mod.len() == 0{
                            println!("-1");
                            return;
                        }
                        let val_1mod = rest_1mod.pop().unwrap();
                        used[val_1mod] = true;
                        ans[*big_node] = val_1mod;
                    }
                }else{
                    let val_1mod = rest_1mod.pop().unwrap();
                    used[val_1mod] = true;
                    ans[*node] = val_1mod;
                    for big_node in bigger_nodes{
                        if rest_2mod.len() == 0{
                            println!("-1");
                            return;
                        }
                        let val_2mod = rest_2mod.pop().unwrap();
                        used[val_2mod] = true;
                        ans[*big_node] = val_2mod;
                    }
                }
            }else{
                println!("-1");
                return;
            }
        }
    }

    let mut notused = used.into_iter()
        .enumerate()
        .filter(|(_, x)| *x == false)
        .map(|(i, _)| i)
        .collect::<Vec<usize>>();


    for i in 1..n+1{
        if ans[i] == 0{
            ans[i] = notused.pop().unwrap();
        }
        print!("{} ", ans[i]);
    }
    println!("");
}

*/

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