Problem:
Score : 900 points

Problem Statement

Takahashi has an ability to generate a tree using a permutation (p_1,p_2,...,p_n) of (1,2,...,n), in the following process:

First, prepare Vertex 1, Vertex 2, ..., Vertex N.
For each i=1,2,...,n, perform the following operation:

If p_i = 1, do nothing.

If p_i \neq 1, let j' be the largest j such that p_j < p_i. Span an edge between Vertex i and Vertex j'.

Takahashi is trying to make his favorite tree with this ability.
His favorite tree has n vertices from Vertex 1 through Vertex n, and its i-th edge connects Vertex v_i and w_i.
Determine if he can make a tree isomorphic to his favorite tree by using a proper permutation.
If he can do so, find the lexicographically smallest such permutation.

Notes

For the definition of isomorphism of trees, see wikipedia. Intuitively, two trees are isomorphic when they are the "same" if we disregard the indices of their vertices.

Constraints

2 \leq n \leq 10^5

1 \leq v_i, w_i \leq n

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

n
v_1 w_1
v_2 w_2
:
v_{n-1} w_{n-1}

Output

If there is no permutation that can generate a tree isomorphic to Takahashi's favorite tree, print -1.
If it exists, print the lexicographically smallest such permutation, with spaces in between.

Sample Input 1

6
1 2
1 3
1 4
1 5
5 6

Sample Output 1

1 2 4 5 3 6

If the permutation (1, 2, 4, 5, 3, 6) is used to generate a tree, it looks as follows:

This is isomorphic to the given graph.

Sample Input 2

6
1 2
2 3
3 4
1 5
5 6

Sample Output 2

1 2 3 4 5 6

Sample Input 3

15
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15

Sample Output 3

-1

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

use std::io::{self, BufRead};
use std::collections::BTreeSet;

fn _dfs(tree: &Vec<BTreeSet<usize>>, visited: &mut Vec<bool>, parents: &mut Vec<usize>, node: usize) -> ((usize, usize), (usize, usize, usize, usize)) {
  visited[node] = true;
  let count = tree[node].iter().filter(|c| !visited[**c]).count();
  if count == 0 {
    return ((0, node), (0, node, node, node));
  }
  let mut results = Vec::new();
  for &child in &tree[node] {
    if visited[child] {
      continue;
    }
    parents[child] = node;
    if count == 1 {
      let ((n1, c1), (n2, c2, c3, c4)) = _dfs(tree, visited, parents, child);
      return ((1 + n1, c1), (n2, c2, c3, c4));
    }
    results.push(_dfs(tree, visited, parents, child));
  }
  results.sort_by(|x, y| (y.0).0.cmp(&(x.0).0));
  let ((n11, c11), (n2, c2, c3, c4)) = results[0];
  let ((n12, c12), _) = results[1];
  if n2 < 2 + n11 + n12 {
    ((1 + n11, c11), (2 + n11 + n12, node, c11, c12))
  } else {
    ((1 + n11, c11), (n2, c2, c3, c4))
  }
}

fn dfs(tree: &Vec<BTreeSet<usize>>, visited: &mut Vec<bool>, parents: &mut Vec<usize>, root: usize) -> (usize, usize, usize, usize) {
  let ((n1, c1), (n2, c2, c3, c4)) = _dfs(tree, visited, parents, root);
  if n1 < n2 {
    (n2, c2, c3, c4)
  } else {
    (n1, root, c1, root)
  }
}

fn find_path(parents: &Vec<usize>, root: usize, leaf1: usize, leaf2: usize) -> Vec<usize> {
  let mut path = Vec::new();
  let mut p = leaf1;
  while p != root {
    path.push(p);
    p = parents[p];
  }
  path.push(p);
  if leaf2 != root {
    let mut path2 = Vec::new();
    let mut p = leaf2;
    while p != root {
      path2.push(p);
      p = parents[p];
    }
    for p in path2.iter().rev() {
      path.push(*p);
    }
  }
  path
}

fn main() {
  let stdin = io::stdin();
  let mut lines = stdin.lock().lines();
  let n: usize = lines.next().unwrap().unwrap().parse().unwrap();
  let mut tree: Vec<BTreeSet<usize>> = vec![BTreeSet::new(); n];
  for _ in 0..(n-1) {
    let xs: Vec<usize> = lines.next().unwrap().unwrap().split(' ').map(|x| x.parse().unwrap()).collect();
    let (u, v) = (xs[0] - 1, xs[1] - 1);
    tree[u].insert(v);
    tree[v].insert(u);
  }
  let mut visited = vec![false; n];
  let mut parents = vec![0; n];
  let (_, root, leaf1, leaf2) = dfs(&tree, &mut visited, &mut parents, 0);
  let path = find_path(&parents, root, leaf1, leaf2);
  let mut answer: Vec<usize> = vec![1];
  for i in 1..(path.len()-1) {
    let valid = tree[path[i]].iter().all(|k| *k == path[i-1] || *k == path[i+1] || tree[*k].len() == 1);
    if !valid {
      println!("-1");
      return;
    }
    let base = answer.len() + 1;
    let count = tree[path[i]].len() - 2;
    for i in 0..count {
      answer.push(base + i + 1);
    }
    answer.push(base);
  }
  let z = answer.len() + 1;
  answer.push(z);
  print!("{}", answer[0]);
  for i in 1..answer.len() {
    print!(" {}", answer[i]);
  }
  println!();
}