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)

//#define debug

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

#define SZ(x) ((int)x.size())

const int N = (int)1e5+5;
int n, sz[N], sz_up[N], par[N];
int a[N], sum[N], new_par[N], cnt[N];
bool dp[N], dp2[N];
vector<int> g[N], res;

void dfs1(int u, int p) {
	par[u] = p, dp[u] = true, sz[u] = 1;
	for (int &v : g[u]) {
		if (v == p) continue;
		dfs1(v, u);
		dp[u] = dp[u] && dp[v];
		sum[u] += dp[v];
	}
	if (cnt[u] > 1) {
		dp[u] = false;
	}
}

void dfs2(int u, int p) {
	if (!dp2[u]) return;
	for (int &v : g[u]) {
		if (v == p) continue;
		sz_up[v] = sz_up[u] + sz[u] - sz[v];
		dp2[v] = dp2[u] && (sum[u] - dp[v] == SZ(g[u]) - (u != 1) - 1);
		if (cnt[u] - (sz[v] > 1) + (sz_up[u] > 1) > 1) dp2[v] = false;
		dfs2(v, u);
	}
}

void dfs_sz(int u, int p) {
	sz[u] = 1;
	for (int &v : g[u]) {
		if (v == p) continue;
		dfs_sz(v, u);
		sz[u] += sz[v];
		cnt[u] += (sz[v] > 1);
	}
}

void dfs_ans(int u, int p, int &mx, vector<int> &re) {
	int children = SZ(g[u]) - (p != 0);
	int last = re.back(); re.pop_back();
	for (int i = 1; i < children; ++i) {
		re.push_back(++mx);
	}
	re.push_back(last), re.push_back(++mx);
	for (int &v : g[u]) {
		if (v == p || sz[v] <= 1) continue;
		dfs_ans(v, u, mx, re);
	}
}

void get_ans(int from) {
	dfs_sz(from, 0);
	vector<int> re;
	re.push_back(1);
	int mx = 1;
	dfs_ans(from, 0, mx, re);
	if (!SZ(res)) {
		res = re;
	} else {
		for (int i = 0; i < n; ++i) {
			if (res[i] != re[i]) {
				if (res[i] > re[i]) {
					res = re;
				}
				return;
			}
		}
	}
}

//void dfs1(int u, int p, int &mx_d) {
//	if (mx_d == 0 || dep[u] > dep[mx_d]) mx_d = u;
//	for (int &v : g[u]) {
//		if (v == p) continue;
//		dep[v] = dep[u] + 1;
//		dfs1(v, u, mx_d);
//		sz[u] += sz[v];
//	}
//	++sz[u];
//}

//bool check_caterpillar(int u, int p) {
//	if (sz[u] == 1) {
//		return true;
//	}
//	int cnt = 0;
//	for (int &v : g[u]) {
//		if (v == p) continue;
//		cnt += (sz[v] > 1);
//		if (cnt > 1) {
//			return false;
//		}
//		if (!check_caterpillar(v, u)) {
//			return false;
//		}
//	}
//	return true;
//}

int main() {
#ifdef debug
	clock_t tStart = clock();
#endif
	scanf("%d", &n);
	for (int i = 1, u, v; i < n; ++i) {
		scanf("%d%d", &u, &v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs1(1, 0);
	dfs_sz(1, 0);
	dp2[1] = true;
	dfs2(1, 0);
	bool ok = false;
	for (int i = 1; i <= n; ++i) {
		if (i == 1 && !(SZ(g[i]) == 1 && dp[i])) continue;
		if (i > 1 && !(dp2[i] && SZ(g[i]) == 1)) continue;
		a[par[i]] = i, ok = true;
	}
	if (!ok) return puts("-1"), 0;
	for (int i = 0; i <= n; ++i) {
		if (!a[i]) continue;
		get_ans(a[i]);
	}
//	int head = 0, tail = 0;
//	dfs1(1, 0, head);
//	memset(dep, 0, sizeof(dep));
//	memset(sz, 0, sizeof(sz));
//	dfs1(head, 0, tail);
//	if (!check_caterpillar(head, 0)) {
//		return puts("-1"), 0;
//	}
//	get_ans(head);
//	get_ans(tail);
	for (int &v : res) {
		printf("%d ", v);
	}
	printf("\n");
#ifdef debug
	printf("Total time: %d ms\n", (int)(1000.0 * (double)(clock() - tStart) / CLOCKS_PER_SEC));
#endif
	return 0;
}