Language: C++

#include <iostream>
#include <algorithm>
#include <vector>
#include <tuple>

template <class Cost = int>
struct Edge {
    int from, to;
    Cost cost;
    Edge(int from = -1, int to = -1, Cost cost = 1)
        : from(from), to(to), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
class Graph {
public:
    int size;
    std::vector<std::vector<Edge<Cost>>> path;

    explicit Graph(int N = 0) : size(N), path(size) {}
    void span(int from, int to, Cost cost = 1) {
        path[from].push_back(Edge<Cost>(from, to, cost));
    }
    std::vector<Edge<Cost>>& operator[](int v) { return path[v]; }
};


using namespace std;

vector<int> build(vector<int>& arr) {
    int cur = 1;  // 次に使われるもの
    vector<int> ret;
    for (auto d : arr) {
        for (int i = 1; i <= d; ++i) {
            ret.push_back(cur + i);
        }
        ret.push_back(cur);
        if (cur == 1) swap(ret.front(), ret.back());
        cur += d + 1;
    }
    swap(ret[ret.size() - 2], ret[ret.size() - 1]);
    return ret;
}

int main() {
    int N;
    cin >> N;

    if (N == 2) {
        // 葉を除くと何もなくなるので例外処理
        cout << 1 << " " << 2 << endl;
        return 0;
    }

    vector<pair<int, int>> edges(N - 1);
    vector<int> deg(N, 0);
    for (auto& e : edges) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        e = make_pair(u, v);
        ++deg[u], ++deg[v];
    }

    // 葉を取り除いたグラフ
    Graph<> path(N);
    vector<int> ch(N, 0);
    for (auto e : edges) {
        int u, v;
        tie(u, v) = e;
        if (deg[u] > deg[v]) swap(u, v);
        if (deg[u] == 1) {
            ++ch[v];
        } else {
            path.span(u, v);
            path.span(v, u);
        }
    }

    // 存在判定 葉を除いたグラフがpathかどうか
    for (int v = 0; v < N; ++v) {
        if (path[v].size() > 2) {
            cout << -1 << endl;
            return 0;
        }
    }

    // pathの端点からDFSして、葉の数の配列を作る
    int r;
    for (r = 0; r < N && (ch[r] == 0 || path[r].size() > 1); ++r) {}

    vector<int> arr;
    function<void(int, int)> dfs = [&](int v, int p) {
        arr.push_back(ch[v]);
        for (auto e : path[v]) {
            if (e.to != p) dfs(e.to, v);
        }
    };
    dfs(r, -1);

    // 両端から順列を作って小さい方が答え
    auto ans = build(arr);
    reverse(arr.begin(), arr.end());
    auto revans = build(arr);
    if (revans < ans) ans = revans;

    for (int i : ans) cout << i << " ";
    cout << endl;

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:96:5: error: 'function' was not declared in this scope
   96 |     function<void(int, int)> dfs = [&](int v, int p) {
      |     ^~~~~~~~
a.cc:4:1: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
    3 | #include <vector>
  +++ |+#include <functional>
    4 | #include <tuple>
a.cc:96:27: error: expression list treated as compound expression in functional cast [-fpermissive]
   96 |     function<void(int, int)> dfs = [&](int v, int p) {
      |                           ^
a.cc:96:14: error: expected primary-expression before 'void'
   96 |     function<void(int, int)> dfs = [&](int v, int p) {
      |              ^~~~
a.cc:102:5: error: 'dfs' was not declared in this scope
  102 |     dfs(r, -1);
      |     ^~~