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)

#include<algorithm>
#include<cmath>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<sstream>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include"rprint2.hpp"
#include"debug_deque.hpp"
#define vector DebugDeque
#else
#define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
FUNC(prints) FUNC(printe) FUNC(printw) FUNC(printew) FUNC(printb) FUNC(printd) FUNC(printde);
#endif
template <class S, class T>
istream& operator >> (istream& in, pair<S, T>& p){ in >> p.first >> p.second; return in; }
template <class T>
istream& operator >> (istream& in, vector<T>& v){ for(auto& e : v){ in >> e; } return in; }
template <char d = ' ', class T>
ostream& operator << (ostream& out, vector<T>& v){ for(auto& e : v){ out << e << d; } return out; }

struct Graph{
    struct Node{
        vector<int> dests;
    };
    vector<Node> nodes;
    Graph(int num): nodes(num){ }
    int id(Node* n){ return n - &nodes[0]; }
    int id(Node& n){ return &n - &nodes[0]; }
    void print(){ for(auto& node : nodes){ prints(id(node), node.dests); } }
    void addEdge(int from, int dest){
        nodes[from].dests.push_back(dest);
    }
    void addEdge2(int from, int dest){
        addEdge(from, dest);
        addEdge(dest, from);
    }
    int dfs1(int idx, int from = -1){
        if(nodes[idx].dests.size() != 2){
            return 0;
        }
        for(int didx : nodes[idx].dests){
            if(didx == from){ continue; }
            return dfs1(didx, idx) + 1;
        }
        return 0;
    }
    void dfs(int idx, int from = -1, int num = 1){
        int cnt = 1, next = -1;
        vector<int> nums;
        for(int didx : nodes[idx].dests){
            if(didx == from){ continue; }
            if(nodes[didx].dests.size() == 1){
                nums.push_back(num + cnt);
                // cout << num + cnt << ' ';
                cnt++;
            }else{
                next = didx;
            }
        }
        int back = 0;
        if(nums.size()){
            if(next == -1){
                back = nums.back(); nums.pop_back();
            }
            cout << nums;
        }
        cout << num << ' ';
        if(back){
            cout << back << ' ';
        }
        if(next != -1){
            dfs(next, idx, num + cnt);
        }
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    Graph g(n);
    for(int i = 0; i < n - 1; i++){
        int v, w;
        cin >> v >> w;
        v--; w--;
        g.addEdge2(v, w);
    }
    bool ok = true;
    int root = -1;
    int maxi = -1;
    for(int i = 0; i < n; i++){
        int cnt = 0;
        for(auto& dest : g.nodes[i].dests){
            cnt += g.nodes[dest].dests.size() >= 2;
        }
        ok &= cnt <= 2;
        //TODO
        // if(root == -1 && cnt <= 1 && g.nodes[i].dests.size() == 1){
        if(cnt <= 1 && g.nodes[i].dests.size() == 1){
            if(maxi < g.dfs1(i)){
                maxi = g.dfs1(i);
                root = i;
            }
            // root = i;
        }
    }
    // prints(ok);
    if(!ok){
        cout << -1 << '\n';
        return 0;
    }
    g.dfs(root);
}