Language: C++

#include <iostream>
#include <vector>
#include <utility>
#define pii pair<int,int>
#define F first
#define S second
using namespace std;

const int maxN = 2e5 + 10;

int N, u, v;
vector<int> adj[maxN];
vector<int> ans, dia;

pii getFar(int p, int u){
    pii pp = {0, u};
    for(int v : adj[u]){
        if(v == p) continue;
        pp = max(getFar(u, v), pp);
    }
    pp.F++;
    //cout << "Res for u = " << u << ": " << pp.F << ", " << pp.S << endl;
    return pp;
}

inline void getDia(int a){
    int current = a, prev = a;
    dia.push_back(a);
    current = adj[current][0];
    bool hasFound;
    while(true){
        //cout << "Current = " << current << endl;
        dia.push_back(current);
        hasFound = false;
        if(adj[current].size() == 1) break;
        else{
            for(int u : adj[current]){
                if(u == prev) continue;
                if(adj[u].size() > 1){
                    prev = current;
                    current = u;
                    hasFound = true;
                    break;
                }
                if(!hasFound){
                    for(int u : adj[current]){
                        if(adj[u].size() == 1){
                            current = u;
                            break;
                        }
                    }
                }
            }
        }
    }
}

vector<int> getPer(vector<int> dia){
    bool isRoot = true;
    vector<int> res = vector<int>(N);
    int currentId = 0, cnt = 1, sz, lim;
    for(int u : dia){
        //cout << "Checking u = " << u << endl;

        if(adj[u].size() == 1){
            sz = adj[u].size() - 1;
        } else {
            sz = adj[u].size() - 2;
        }
        //cout << "sz = " << sz << endl;
        res[currentId + sz] = cnt++;
        lim = currentId + sz;
        for(; currentId < lim; currentId++) res[currentId] = cnt++;
        currentId++;
        //for(int u : res) cout << u << " "; cout << endl;
    }
    return res;
}

bool isLess(vector<int> a, vector<int> b){
    for(int i = 0; i < N; i++){
        if(a[i] < b[i]) return true;
        else if(a[i] > b[i]) return false;
    }
}

int main(){
    cin >> N;
    for(int i = 0; i < N - 1; i++){
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    pii p1 = getFar(1, 1), p2;
    int a = p1.S;
    p2 = getFar(a, a);
    int b = p2.S;
    // cout << a << " " << b << endl;
    getDia(a);
    //cout << "DIA: " << endl;
    //for(int u : dia) cout << u << " "; cout << endl;
    vector<int> res1 = getPer(dia);
    for(int u : res1){
        if(!u){
            cout << -1 << endl;
            return 0;
        }
    }
    reverse(dia.begin(), dia.end());
    vector<int> res2 = getPer(dia);
    if(isLess(res1, res2)){
        for(int u : res1) cout << u << " ";
    } else {
        for(int u : res2) cout << u << " ";
    }
    cout << endl;

}


Compiler output:
a.cc: In function 'int main()':
a.cc:109:5: error: 'reverse' was not declared in this scope
  109 |     reverse(dia.begin(), dia.end());
      |     ^~~~~~~
a.cc: In function 'bool isLess(std::vector<int>, std::vector<int>)':
a.cc:85:1: warning: control reaches end of non-void function [-Wreturn-type]
   85 | }
      | ^