Problem p03384 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize ("O3")
// #pragma GCC target ("tune=native")
// #pragma GCC target ("avx")
// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<P, ll> PPI;
typedef pair<ll, P> PIP;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vp;
#define PQ(T) priority_queue<T, vector<T>, greater<T>>
#define PQ2(T) priority_queue<T>
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL << 29;
const ll mod = 1e9 + 7;
#define REP(i, a, b) for (ll(i) = a; (i) < (ll)(b); ++(i))
#define rep(i, n) REP(i, 0, n)
#define rep1(i, n) REP(i, 1, n + 1)
#define repd(i, n, d) for (ll(i) = 0; (i) < (ll)(n); (i) += (d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset((m), (v), sizeof(m))
#define chmin(x, y) ((x) = min((x), (y)))
#define chmax(x, y) ((x) = max((x), (y)))
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)), (x).end())
#define DEBUG(x)                                                               \
  cerr << "line (" << __LINE__ << ")  " << #x << ": " << x << endl;
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  int n = v.size();
  rep(i, n) os << v[i] << (i == n - 1 ? "" : " ");
  return os;
}
#define INIT                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0);

#define N 11000
vector<int> g[N];
int n;
int d[N];

void dfs(int u) {
  for (auto &&v : g[u]) {
    if (d[v] >= 0)
      continue;
    d[v] = d[u] + 1;
    dfs(v);
  }
}

vector<int> f(int u1, int u2) {
  vector<int> r;
  r.reserve(n);
  int u = g[u1][0], uu = u1;
  r.pb(1);
  int p = 2;
  while (u >= 0) {
    int cnt = g[u].size() - 2, v = -1;
    for (auto &&w : g[u]) {
      if (w != uu && g[w].size() >= 2)
        v = w;
    }
    rep(i, cnt) r.pb(p + 1 + i);
    r.pb(p);
    p += cnt + 1;
    uu = u;
    u = v;
  }
  r.pb(p);
  return r;
}

int main() {
  INIT;
  cin >> n;
  if (n == 2) {
    cout << "1 2" << endl;
    return 0;
  }
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].pb(v);
    g[v].pb(u);
  }
  int u1 = 0, u2 = 0;
  mset(d, -1);
  d[0] = 0;
  dfs(0);
  rep(i, n) if (d[i] > d[u1]) u1 = i;
  mset(d, -1);
  d[u1] = 0;
  dfs(u1);
  rep(i, n) if (d[i] > d[u2]) u2 = i;
  vector<int> r1 = f(u1, u2), r2 = f(u2, u1);
  if (r1.size() < n) {
    cout << -1 << endl;
    return 0;
  }
  if (r1 > r2)
    r1.swap(r2);
  cout << r1 << endl;
  return 0;
}


Fixed submission:
// #pragma GCC optimize ("O3")
// #pragma GCC target ("tune=native")
// #pragma GCC target ("avx")
// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<P, ll> PPI;
typedef pair<ll, P> PIP;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vp;
#define PQ(T) priority_queue<T, vector<T>, greater<T>>
#define PQ2(T) priority_queue<T>
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL << 29;
const ll mod = 1e9 + 7;
#define REP(i, a, b) for (ll(i) = a; (i) < (ll)(b); ++(i))
#define rep(i, n) REP(i, 0, n)
#define rep1(i, n) REP(i, 1, n + 1)
#define repd(i, n, d) for (ll(i) = 0; (i) < (ll)(n); (i) += (d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset((m), (v), sizeof(m))
#define chmin(x, y) ((x) = min((x), (y)))
#define chmax(x, y) ((x) = max((x), (y)))
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)), (x).end())
#define DEBUG(x)                                                               \
  cerr << "line (" << __LINE__ << ")  " << #x << ": " << x << endl;
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  int n = v.size();
  rep(i, n) os << v[i] << (i == n - 1 ? "" : " ");
  return os;
}
#define INIT                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0);

#define N 110000
vector<int> g[N];
int n;
int d[N];

void dfs(int u) {
  for (auto &&v : g[u]) {
    if (d[v] >= 0)
      continue;
    d[v] = d[u] + 1;
    dfs(v);
  }
}

vector<int> f(int u1, int u2) {
  vector<int> r;
  r.reserve(n);
  int u = g[u1][0], uu = u1;
  r.pb(1);
  int p = 2;
  while (u >= 0) {
    int cnt = g[u].size() - 2, v = -1;
    for (auto &&w : g[u]) {
      if (w != uu && g[w].size() >= 2)
        v = w;
    }
    rep(i, cnt) r.pb(p + 1 + i);
    r.pb(p);
    p += cnt + 1;
    uu = u;
    u = v;
  }
  r.pb(p);
  return r;
}

int main() {
  INIT;
  cin >> n;
  if (n == 2) {
    cout << "1 2" << endl;
    return 0;
  }
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].pb(v);
    g[v].pb(u);
  }
  int u1 = 0, u2 = 0;
  mset(d, -1);
  d[0] = 0;
  dfs(0);
  rep(i, n) if (d[i] > d[u1]) u1 = i;
  mset(d, -1);
  d[u1] = 0;
  dfs(u1);
  rep(i, n) if (d[i] > d[u2]) u2 = i;
  vector<int> r1 = f(u1, u2), r2 = f(u2, u1);
  if (r1.size() < n) {
    cout << -1 << endl;
    return 0;
  }
  if (r1 > r2)
    r1.swap(r2);
  cout << r1 << endl;
  return 0;
}