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:
/*
#![allow(bare_trait_objects)]
#![allow(unused_imports)]
#![allow(dead_code)]

use std::fmt::Debug;
use std::io::BufRead;
use std::io::StdinLock;
use std::str::FromStr;


pub struct Scanner<'a> {
    buf_read: Box<dyn BufRead + 'a>,
    tokens: Vec<String>,
}

impl<'a> Scanner<'a> {
    pub fn new(buf_read: StdinLock<'a>) -> Scanner {
        Scanner {
            buf_read: Box::new(buf_read),
            tokens: Vec::new(),
        }
    }

    pub fn next_str(&mut self) -> String {
        while self.tokens.is_empty() {
            let mut s = String::new();
            if self.buf_read.read_line(&mut s).is_err() {
                panic!()
            }
            self.tokens = s
                .split_whitespace()
                .filter(|s| !s.is_empty())
                .rev()
                .map(str::to_string)
                .collect();
        }
        self.tokens.pop().unwrap()
    }

    pub fn next<T: FromStr>(&mut self) -> T
    where
        T::Err: Debug,
    {
        while self.tokens.is_empty() {
            let mut s = String::new();
            if self.buf_read.read_line(&mut s).is_err() {
                panic!()
            }
            self.tokens = s
                .split_whitespace()
                .filter(|s| !s.is_empty())
                .rev()
                .map(str::to_string)
                .collect();
        }
        self.tokens.pop().unwrap().parse().unwrap()
    }

    pub fn usize(&mut self) -> usize
    {
        self.next()
    }

    pub fn i64(&mut self) -> i64
    {
        self.next()
    }
}

struct Tree {
    n: usize,
    edges: Vec<Vec<usize>>,
    parent: Vec<Option<usize>>,
    rorder: Vec<usize>,
}

impl Tree {
    fn read(sc: &mut Scanner, n: usize) -> Self {
        let mut edges = vec![Vec::new(); n];
        for _ in 0..n-1 {
            let u = sc.usize() - 1;
            let v = sc.usize() - 1;
            edges[u].push(v);
            edges[v].push(u);
        }
        let mut t = Tree { n, edges, parent: vec![None; n], rorder: Vec::new(), };
        t.init();
        t
    }

    fn init(&mut self) {
        let mut st = Vec::new();
        st.push(0);
        let mut visited = vec![false; self.n];
        self.rorder.clear();

        while let Some(v) = st.pop() {
            if !visited[v] {
                visited[v] = true;
                self.rorder.push(v);
                for &t in &self.edges[v] {
                    if !visited[t] {
                        st.push(t);
                        self.parent[t] = Some(v);
                    }
                }
            }
        }

        self.edges.iter_mut().for_each(|es| es.clear());
        for v in 0..self.n {
            if let Some(p) = self.parent[v] {
                self.edges[p].push(v);
            }
        }

        self.rorder.reverse();
    }

    fn order<'a>(&'a self) -> impl Iterator<Item=&usize> + 'a {
        self.rorder.iter().rev()
    } 

    // fn rorder(&self) -> impl Iterator {
    //     self.rorder.iter()
    // } 
}

fn print_list<T: ToString>(x: &[T]) {
    println!("{}", x.iter().map(T::to_string).collect::<Vec<_>>().join(" "));
}

fn main() {
    let stdin = std::io::stdin();
    let mut sc = Scanner::new(stdin.lock());
    let n = sc.usize();

    let tree =  Tree::read(&mut sc, n);
    let mut depth = vec![0; n];
    let mut set = vec![Vec::new(); 2];
    let mut mod3 = vec![Vec::new(); 3];

    for i in 1..=n {
        mod3[i%3].push(i);
    }

    for &v in tree.order() {
        if let Some(p) = tree.parent[v] {
            depth[v] = depth[p] + 1;
        }
        set[depth[v] % 2].push(v);
    }

    if set[0].len() < set[1].len() {
        set.swap(0, 1);
    }

    let mut ans = vec![0; n];
    if set[1].len() <= mod3[0].len() {
        for &v in &set[1] {
            ans[v] = mod3[0].pop().unwrap();
        }
        let mut all = Vec::new();
        mod3.into_iter().for_each(|mut s| all.append(&mut s));
        for &v in &set[0] {
            ans[v] = all.pop().unwrap();
        }
    } else {
        for i in 0..2 {
            for &v in &set[i] {
                if let Some(x) = mod3[i+1].pop() {
                    ans[v] = x;
                } else {
                    ans[v] = mod3[0].pop().unwrap();
                }
            }
        }
    }
    // println!("{}", ans.iter().map(usize::to_string).collect::<Vec<_>>().join(" "));
    print_list(&ans);
    // println!("{}", ans.join(" "));
}
*/

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