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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

//by tzupengwang™
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<iostream>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;

int n;
int s[100005];
int dg[100005];
vector<int> v[100005];
bool lf[100005];

void init() {
  scanf("%d", &n);
  for (int i = 1; i < n; i ++) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
}

void bad() {
  puts("-1");
  exit(0);
}

bool vis[100005];
void dfs(int x, vector<int> &vv) {
  vis[x] = true;
  int cnt = 0, nxt = -1;
  for (int y : v[x]) {
    if (lf[y]) cnt ++;
    else if (!vis[y]) nxt = y;
  }

  vv.push_back(cnt);
  if (nxt != -1)
    dfs(nxt, vv);
}

void process() {
  bool g2 = false;
  for (int i = 1; i <= n; i ++) {
    if (v[i].size() == 1u)
      lf[i] = true;
    if (v[i].size() > 2u) g2 = true;
  }

  if (!g2) {
    for (int i = 1; i <= n; i ++) printf("%d%c", i, " \n"[i == n]);
    return;
  }

  bool flag = true;
  vector<int> dg1, dg2, zer;
  for (int i = 1; i <= n; i ++) if (!lf[i]) {
    int td = 0;
    for (int j : v[i]) if (!lf[j]) {
      td ++;
    }
    if (td > 2) flag = false;
    else if (td == 2) dg2.push_back(i);
    else if (td == 1) dg1.push_back(i);
    else zer.push_back(i);
  }

  if (!flag) bad();
  if (zer.size()) {
    if (dg1.size() || dg2.size()) bad();
    printf("1");
    for (int i = 3; i <= n; i ++) printf(" %d", i);
    printf(" 2\n");
    return;
  }

  if (dg1.size() != 2) bad();
  vector<int> vt[2];
  memset(vis, false, sizeof vis);
  dfs(dg1[0], vt[0]);
  memset(vis, false, sizeof vis);
  dfs(dg1[1], vt[1]);

  int bt = 0;
  for (int i = 0; i < (int)vt[1].size(); i ++) {
    if (vt[0][i] > vt[1][i]) {
      bt = 1;
      break;
    } else if (vt[0][i] < vt[1][i]) {
      bt = 0;
      break;
    }
  }

  vector<int> &vv = vt[bt];
  printf("1");
  vv[0] --;
  vv.back() --;
  int prt = 2;
  for (int i = 0; i < (int)vv.size(); i ++) {
    for (int j = 0; j < vv[i]; j ++)
      printf(" %d", prt + j + 1);
    printf(" %d", prt);
    prt += vv[i] + 1;
  }
  printf(" %d\n", n);
}

int main() {
/*
#ifdef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
#endif
  int Cases;
  scanf("%d", &Cases);
  for (int cs = 1; cs <= Cases; cs ++) {
*/
    init();
    process();
/*
  }
*/
  return 0;
}