Problem p03384 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define f first
#define s second
#define MAXN 100100

int A[MAXN];
int gone[MAXN];
vi V[MAXN];
vi V2[MAXN];
int N, a, b;
vpi E;
vi res, res2;
deque<int> l;

void dfs(int x, int p) {
  // cout<<"Safe "<<x<<'\n';
  l.pb(A[x]);
  for (auto v : V[x])
    if (v != p && !gone[v]) {
      dfs(v, x);
    }
}

int main() {
  cin >> N;
  if (N == 2) {
    cout << "1 2";
    return 0;
  }
  for (int i = 1; i < N; ++i) {
    cin >> a >> b;
    E.pb(a, b);
    V[a].pb(b);
    V[b].pb(a);
  }
  for (int i = 1; i <= N; ++i)
    if (SZ(V[i]) == 1)
      gone[i] = 1;
  for (auto i : E) {
    if (gone[i.f]) {
      ++A[i.s];
    } else if (gone[i.s]) {
      ++A[i.f];
    }
  }
  // for (int i=1;i<=N;++i)cout<<gone[i]<<' ';cout<<'\n';
  for (auto i : E) {
    // cout<<i.f<<' '<<i.s<<' '<<gone[i.f]<<' '<<gone[i.s]<<'\n';
    if (gone[i.s] || gone[i.f])
      continue;
    V2[i.f].pb(i.s);
    V2[i.s].pb(i.f);
    // cout<<i.f<<' '<<i.s<<'\n';
  }
  for (int i = 1; i <= N; ++i)
    if (SZ(V2[i]) > 2) {
      cout << -1;
      return 0;
    }
  for (int i = 1; i <= N; ++i) {
    if (SZ(V2[i]) == 1) {
      dfs(i, -1);
      break;
    }
  }

  l.back()--;
  l.push_back(0);
  l[0]--;
  l.push_front(0);

  int t = 1;
  for (auto i : l) {
    // cout<<"Do "<<i<<'\n';
    if (i == 0) {
      res.pb(t);
      ++t;
      continue;
    }
    if (i == 1) {
      res.pb(t + 1);
      res.pb(t);
      t += 2;
      continue;
    }
    for (int k = t + 1; k <= t + i; ++k) {
      res.pb(k);
    }
    res.pb(t);
    t = t + i + 1;
  }

  reverse(ALL(l));

  t = 1;
  for (auto i : l) {
    if (i == 0) {
      res2.pb(t);
      ++t;
      continue;
    }
    if (i == 1) {
      res2.pb(t + 1);
      res2.pb(t);
      t += 2;
      continue;
    }
    for (int k = t + 1; k <= t + i; ++k) {
      res2.pb(k);
    }
    res2.pb(t);
    t = t + i + 1;
  }
  // for (auto i :res2)cout<<i<<' ';cout<<'\n';
  bool btr = 0;
  for (int i = 0; i < N; ++i) {
    if (btr)
      cout << res[i] << ' ';
    else if (res[i] == res2[i]) {
      cout << res[i] << ' ';
    } else if (res[i] > res2[i]) {
      --i;
      swap(res, res2);
      btr = 1;
    } else {
      --i;
      btr = 1;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define f first
#define s second
#define MAXN 100100

int A[MAXN];
int gone[MAXN];
vi V[MAXN];
vi V2[MAXN];
int N, a, b;
vpi E;
vi res, res2;
deque<int> l;

void dfs(int x, int p) {
  // cout<<"Safe "<<x<<'\n';
  l.pb(A[x]);
  for (auto v : V[x])
    if (v != p && !gone[v]) {
      dfs(v, x);
    }
}

int main() {
  cin >> N;
  if (N == 2) {
    cout << "1 2";
    return 0;
  }
  for (int i = 1; i < N; ++i) {
    cin >> a >> b;
    E.pb(a, b);
    V[a].pb(b);
    V[b].pb(a);
  }
  for (int i = 1; i <= N; ++i)
    if (SZ(V[i]) == 1)
      gone[i] = 1;
  for (auto i : E) {
    if (gone[i.f]) {
      ++A[i.s];
    } else if (gone[i.s]) {
      ++A[i.f];
    }
  }
  // for (int i=1;i<=N;++i)cout<<gone[i]<<' ';cout<<'\n';
  for (auto i : E) {
    // cout<<i.f<<' '<<i.s<<' '<<gone[i.f]<<' '<<gone[i.s]<<'\n';
    if (gone[i.s] || gone[i.f])
      continue;
    V2[i.f].pb(i.s);
    V2[i.s].pb(i.f);
    // cout<<i.f<<' '<<i.s<<'\n';
  }
  for (int i = 1; i <= N; ++i)
    if (SZ(V2[i]) > 2) {
      cout << -1;
      return 0;
    }
  for (int i = 1; i <= N; ++i) {
    if (SZ(V2[i]) == 1) {
      dfs(i, -1);
      break;
    }
  }
  if (SZ(l) == 0) {
    // Only one node
    for (int i = 1; i <= N; ++i) {
      if (SZ(V[i]) > 1) {
        dfs(i, -1);
        break;
      }
    }
  }

  l.back()--;
  l.push_back(0);
  l[0]--;
  l.push_front(0);

  int t = 1;
  for (auto i : l) {
    // cout<<"Do "<<i<<'\n';
    if (i == 0) {
      res.pb(t);
      ++t;
      continue;
    }
    if (i == 1) {
      res.pb(t + 1);
      res.pb(t);
      t += 2;
      continue;
    }
    for (int k = t + 1; k <= t + i; ++k) {
      res.pb(k);
    }
    res.pb(t);
    t = t + i + 1;
  }

  reverse(ALL(l));

  t = 1;
  for (auto i : l) {
    if (i == 0) {
      res2.pb(t);
      ++t;
      continue;
    }
    if (i == 1) {
      res2.pb(t + 1);
      res2.pb(t);
      t += 2;
      continue;
    }
    for (int k = t + 1; k <= t + i; ++k) {
      res2.pb(k);
    }
    res2.pb(t);
    t = t + i + 1;
  }
  // for (auto i :res2)cout<<i<<' ';cout<<'\n';
  bool btr = 0;
  for (int i = 0; i < N; ++i) {
    if (btr)
      cout << res[i] << ' ';
    else if (res[i] == res2[i]) {
      cout << res[i] << ' ';
    } else if (res[i] > res2[i]) {
      --i;
      swap(res, res2);
      btr = 1;
    } else {
      --i;
      btr = 1;
    }
  }
}