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 (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <cstdio>
#include <vector>
#include <stack>
#include <iostream>
#include <string>
#include <tuple>
#include <random>
#include <map>
#include <queue>
#include <set>
#include <complex>
#include <algorithm>
#include <cassert>
#include <iterator>
#include <numeric>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;

const ll INF = 1e15;
const double eps = 1e-6;
const ll MOD = 1000000007;

using Weight = ll;
class Edge {
public:
	int s, d;
	Weight w;
	Edge() : s(0), d(0), w(0) {}
	Edge(int s, int d, Weight w) : s(s), d(d), w(w) {}
	bool operator < (const Edge &e) const {
		return w != e.w ? w < e.w :
			s != e.s ? s < e.s :
			d < e.d;
	}
};
using Graph = vector<vector<Edge>>;

// 始点sからの単一支点最短距離と最短経路木のペアを返す O(ElogV)
pair<vector<Weight>, Graph> dijkstra(int s, Graph& g) {
	vector<Weight> dist(g.size(), INF);
	vector<pair<int, Weight>> prev(g.size(), make_pair(-1, 0));
	priority_queue<Edge> que;
	que.push(Edge(-1, s, 0));
	while (!que.empty()) {
		Edge now = que.top();
		now.w *= -1;
		que.pop();
		if (dist[now.d] <= now.w)
			continue;
		dist[now.d] = now.w;
		prev[now.d] = make_pair(now.s, now.w);
		for (auto&& e : g[now.d]) {
			if (e.w < dist[e.d])
				que.push(Edge(e.s, e.d, -(e.w + now.w)));
		}
	}
	Graph f(g.size());
	for (int i = 0;i < prev.size();i++)
		if (prev[i].first != -1)
			f[i].push_back(Edge(i, prev[i].first, prev[i].second));
	return make_pair(dist, f);
}


int main() {
	ll N;
	cin >> N;
	Graph g(N);
	for (int i = 0;i < N-1;i++) {
		ll v, w;
		cin >> v >> w;
		v--;
		w--;
		g[v].push_back(Edge(v, w, 1));
		g[w].push_back(Edge(w, v, 1));
	}
	vector<Weight> dist;
	Graph h;
	tie(dist, h) = dijkstra(0, g);
	pair<ll, int> node = P(-1, -1);
	for (int i = 0;i < N;i++)
		node = max(make_pair(dist[i], i), node);
	tie(dist, h) = dijkstra(node.second, g);
	pair<ll, int> node2 = P(-1, -1);
	for (int i = 0;i < N;i++)
		node2 = max(make_pair(dist[i], i), node2);

	vector<int> line(N, false);
	ll now = node2.second;
	line[node.second] = true;
	while (now != node.second) {
		line[now] = true;
		now = h[now][0].d;
	}
	bool ok = true;
	vector<ll> cnt(N);
	for (int i = 0;i < N;i++) {
		bool nowok = false;
		for (int j = 0;j < g[i].size();j++) {
			if (line[g[i][j].d] || line[i])
				nowok = true;
			if (!line[i] && line[g[i][j].d])
				cnt[g[i][j].d]++;
		}
		ok &= nowok;
	}
	if (!ok) {
		cout << -1 << endl;
		return 0;
	}
	vector<ll> ans(N);
	ll l = 0;
	ll r = N - 1;
	ll rest = N;
	ll nownode = node2.second;
	while (rest > 0) {
		ans[r] = rest - cnt[nownode];
		for (int i = 0;i < cnt[nownode];i++) {
			ans[l] = rest - cnt[nownode] + i + 1;
			l++;
		}
		r--;
		rest -= 1 + cnt[nownode];
		if (h[nownode].empty())
			break;
		nownode = h[nownode][0].d;
	}
	for (int i = 0;i < N;i++) {
		cout << ans[i];
		if (i == N - 1)
			cout << endl;
		else cout << " ";
	}
	return 0;
}