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)

#![allow(unused_imports)]
#![allow(non_snake_case, unused)]

use std::cmp::*;
use std::collections::*;
use std::ops::*;

macro_rules! eprint {
	($($t:tt)*) => {{
		use ::std::io::Write;
		let _ = write!(::std::io::stderr(), $($t)*);
	}};
}
macro_rules! eprintln {
	() => { eprintln!(""); };
	($($t:tt)*) => {{
		use ::std::io::Write;
		let _ = writeln!(::std::io::stderr(), $($t)*);
	}};
}
macro_rules! dbg {
	($v:expr) => {{
		let val = $v;
		eprintln!("[{}:{}] {} = {:?}", file!(), line!(), stringify!($v), val);
		val
	}}
}

macro_rules! mat {
	($($e:expr),*) => { Vec::from(vec![$($e),*]) };
	($($e:expr,)*) => { Vec::from(vec![$($e),*]) };
	($e:expr; $d:expr) => { Vec::from(vec![$e; $d]) };
	($e:expr; $d:expr $(; $ds:expr)+) => { Vec::from(vec![mat![$e $(; $ds)*]; $d]) };
}

macro_rules! ok {
	($a:ident$([$i:expr])*.$f:ident()$(@$t:ident)*) => {
		$a$([$i])*.$f($($t),*)
	};
	($a:ident$([$i:expr])*.$f:ident($e:expr$(,$es:expr)*)$(@$t:ident)*) => { {
		let t = $e;
		ok!($a$([$i])*.$f($($es),*)$(@$t)*@t)
	} };
}

pub fn readln() -> String {
	let mut line = String::new();
	::std::io::stdin().read_line(&mut line).unwrap_or_else(|e| panic!("{}", e));
	line
}

macro_rules! read {
	($($t:tt),*; $n:expr) => {{
		let stdin = ::std::io::stdin();
		let ret = ::std::io::BufRead::lines(stdin.lock()).take($n).map(|line| {
			let line = line.unwrap();
			let mut it = line.split_whitespace();
			_read!(it; $($t),*)
		}).collect::<Vec<_>>();
		ret
	}};
	($($t:tt),*) => {{
		let line = readln();
		let mut it = line.split_whitespace();
		_read!(it; $($t),*)
	}};
}

macro_rules! _read {
	($it:ident; [char]) => {
		_read!($it; String).chars().collect::<Vec<_>>()
	};
	($it:ident; [u8]) => {
		Vec::from(_read!($it; String).into_bytes())
	};
	($it:ident; usize1) => {
		$it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1
	};
	($it:ident; [usize1]) => {
		$it.map(|s| s.parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1).collect::<Vec<_>>()
	};
	($it:ident; [$t:ty]) => {
		$it.map(|s| s.parse::<$t>().unwrap_or_else(|e| panic!("{}", e))).collect::<Vec<_>>()
	};
	($it:ident; $t:ty) => {
		$it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<$t>().unwrap_or_else(|e| panic!("{}", e))
	};
	($it:ident; $($t:tt),+) => {
		($(_read!($it; $t)),*)
	};
}

pub fn main() {
	let _ = ::std::thread::Builder::new().name("run".to_string()).stack_size(32 * 1024 * 1024).spawn(run).unwrap().join();
}

const MOD: usize = 1_000_000_007;
const INF: i64 = std::i64::MAX/2;

pub trait SetMinMax {
	fn setmin(&mut self, v: Self) -> bool;
	fn setmax(&mut self, v: Self) -> bool;
}
impl<T> SetMinMax for T where T: PartialOrd {
	fn setmin(&mut self, v: T) -> bool {
		*self > v && { *self = v; true }
	}
	fn setmax(&mut self, v: T) -> bool {
		*self < v && { *self = v; true }
	}
}

fn solve() {
	let n = read!(usize);
	let e = read!([usize];n-1);
	let mut edge = vec![vec![];n];
	for i in 0..n-1 {
		let s = e[i][0]-1;
		let t = e[i][1]-1;
		edge[s].push(t);
		edge[t].push(s);
	}
	let (a,b,_) = diameter(&edge);
	let mut dist = vec![0;n];
	dfs2(a,n,&mut dist,&edge,0);
	dfs2(b,n,&mut dist,&edge,0);
	let mut color = vec![0;n];
	dfs3(a,n,&mut color,&edge,0);
	let mut zerolist = vec![];
	let mut onelist = vec![];
	for i in 0..n {
		if color[i]==0 {
			zerolist.push((i,dist[i],false));
		}
		else {
			onelist.push((i,dist[i],false));
		}
	}
	let mut zeroidx = 0;
	let mut oneidx = 0;
	let mut val = 1;
	let mut seen = vec![false;n+1];
	let mut ans = vec![0;n];
	for i in 0..zerolist.len() {
		let (idx,d,flag) = zerolist[i];
		if d<=2 {
			continue;
		}
		ans[idx] = val;
		zerolist[i].2 = true;
		if val>n {
			break;
		}
		seen[val] = true;
		val += 3;
	}
	val = 2;
	for i in 0..onelist.len() {
		let (idx,d,flag) = onelist[i];
		if d<=2 {
			continue;
		}
		ans[idx] = val;
		onelist[i].2 = true;
		if val>n {
			break;
		}
		seen[val] = true;
		val += 3;
	}
	val = 3;
	for i in 0..zerolist.len() {
		let (idx,d,flag) = zerolist[i];
		if d<=2 || flag {
			continue;
		}
		ans[idx] = val;
		zerolist[i].2 = true;
		if val>n {
			break;
		}
		seen[val] = true;
		val += 3;
	}
	for i in 0..onelist.len() {
		let (idx,d,flag) = onelist[i];
		if d<=2 || flag{
			continue;
		}
		ans[idx] = val;
		onelist[i].2 = true;
		if val>n {
			break;
		}
		seen[val] = true;
		val += 3;
	}
	val = 1;
	for i in 0..zerolist.len() {
		let (idx,d,flag) = zerolist[i];
		if flag || d>=3 {
			continue;
		}
		while val <= n && seen[val] {
			val += 1;
		}
		if val>n {
			break;
		}
		ans[idx] = val;
		zerolist[i].2 = true;
		seen[val] = true;
	}
	for i in 0..onelist.len() {
		let (idx,d,flag) = onelist[i];
		if flag || d>=3 {
			continue;
		}
		while val <= n && seen[val] {
			val += 1;
		}
		if val>n {
			break;
		}
		ans[idx] = val;
		onelist[i].2 = true;
		seen[val] = true;
	}
	// for i in 0..n {
	// 	print!("{} ", color[i]);
	// }
	// println!();
	// for i in 0..n {
	// 	print!("{} ", dist[i]);
	// }
	// println!();
	for i in 0..n {
		if ans[i]==0 {
			println!("-1");
			return;
		}
	}
	for i in 0..n {
		print!("{} ", ans[i]);
	}


}

fn dfs2(s: usize, p: usize, dist: &mut Vec<i64>, edge: &Vec<Vec<usize>>, calc_dist: i64) {
    dist[s].setmax(calc_dist);
    for &e in &edge[s] {
        if p==e {continue;}
        dfs2(e, s, dist, edge, calc_dist+1);
    }
}

fn dfs3(s: usize, p: usize, color: &mut Vec<i64>, edge: &Vec<Vec<usize>>, calc_dist: i64) {
    color[s] = calc_dist%2;
    for &e in &edge[s] {
        if p==e {continue;}
        dfs3(e, s, color, edge, calc_dist+1);
    }
}

fn diameter(edge: &Vec<Vec<usize>>) -> (usize, usize, i64) {
    let n = edge.len();
    let mut result = vec![0;n];
    dfs(0,n,0,&edge,&mut result);
    let (s,_) = result.iter().enumerate().max_by(|x, y| x.1.cmp(y.1)).unwrap();
    result = vec![0; n];
    dfs(s,n,0,&edge,&mut result);
    let (t,val) = result.iter().enumerate().max_by(|x, y| x.1.cmp(y.1)).unwrap(); 
    (s,t,*val)
}

fn dfs(s: usize, p: usize, dist: i64, edge: &Vec<Vec<usize>>, result: &mut Vec<i64>) {
    result[s] = dist;
    for &e in &edge[s] {
        if p==e {continue;}
        dfs(e, s, dist+1, edge, result);
    }
}

fn run() {
    solve();
}