Problem p03384 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define YYS(x, arr) for (auto &x : arr)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort((x).begin(), (x).end())
#define REVERSE(x) reverse((x).begin(), (x).end())
#define UNIQUE(x) (x).erase(unique(ALL((x))), (x).end())
#define PW(x) (1LL << (x))
#define SZ(x) ((ll)(x).size())
#define SHOW(x) cout << #x << " = " << x << endl
#define SHOWA(x, n)                                                            \
  for (int yui = 0; yui < n; yui++) {                                          \
    cout << x[yui] << " ";                                                     \
  }                                                                            \
  cout << endl

#define pb emplace_back
#define fi first
#define se second

using namespace std;

typedef long double ld;
typedef long long int ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vpl> gr;
typedef vector<vl> ml;
typedef vector<vd> md;
typedef vector<vi> mi;

const ll INF = (ll)1e9 + 10;
const ll INFLL = (ll)1e18 + 10;
const ld EPS = 1e-12;
const ll MOD = 1e9 + 7;

template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <class T> inline T sq(T a) { return a * a; }

ll in() {
  long long int x;
  scanf("%lld", &x);
  return x;
}
char yuyushiki[1000010];
string stin() {
  scanf("%s", yuyushiki);
  return yuyushiki;
}

// head

int n;

vi G[100010];
int sz[100010];

vi v;

pi dfs1(int x, int p) {
  pi res = pi(0, x);
  YYS(w, G[x]) {
    if (w != p) {
      pi ret = dfs1(w, x);
      ret.fi++;
      chmax(res, ret);
    }
  }
  return res;
}

int dfs2(int x, int p) {
  sz[x] = 1;
  YYS(w, G[x]) {
    if (w != p) {
      sz[x] += dfs2(w, x);
    }
  }
  return sz[x];
}

void dfs3(int x, int p) {
  int chs = -1;
  int cnt = 0;
  YYS(w, G[x]) {
    if (w != p) {
      if (sz[w] != 1) {
        if (chs == -1) {
          chs = w;
        } else {
          puts("-1");
          exit(0);
        }
      } else {
        cnt++;
      }
    }
  }
  v.pb(cnt);
  if (chs != -1) {
    dfs3(chs, x);
  }
}

int main() {

  n = in();
  REP(i, n - 1) {
    int a = in() - 1;
    int b = in() - 1;
    G[a].pb(b);
    G[b].pb(a);
  }

  int x = dfs1(0, -1).fi;

  dfs2(x, -1);

  v.clear();
  dfs3(x, -1);
  v.back()--;
  v.pb(0);

  vi u = v;
  REVERSE(u);

  assert(v[0] == 0);
  assert(u[0] == 0);

  v = min(v, u);

  vi ans(0);
  int cnt = 1;
  YYS(w, v) {
    int cur = cnt++;
    REP(i, w) { ans.pb(cnt++); }
    ans.pb(cur);
  }

  // assert(SZ(ans) == n);

  REP(i, n) {
    if (i != 0) {
      printf(" ");
    }
    printf("%d", ans[i]);
  }
  printf("\n");

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define YYS(x, arr) for (auto &x : arr)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort((x).begin(), (x).end())
#define REVERSE(x) reverse((x).begin(), (x).end())
#define UNIQUE(x) (x).erase(unique(ALL((x))), (x).end())
#define PW(x) (1LL << (x))
#define SZ(x) ((ll)(x).size())
#define SHOW(x) cout << #x << " = " << x << endl
#define SHOWA(x, n)                                                            \
  for (int yui = 0; yui < n; yui++) {                                          \
    cout << x[yui] << " ";                                                     \
  }                                                                            \
  cout << endl

#define pb emplace_back
#define fi first
#define se second

using namespace std;

typedef long double ld;
typedef long long int ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vpl> gr;
typedef vector<vl> ml;
typedef vector<vd> md;
typedef vector<vi> mi;

const ll INF = (ll)1e9 + 10;
const ll INFLL = (ll)1e18 + 10;
const ld EPS = 1e-12;
const ll MOD = 1e9 + 7;

template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <class T> inline T sq(T a) { return a * a; }

ll in() {
  long long int x;
  scanf("%lld", &x);
  return x;
}
char yuyushiki[1000010];
string stin() {
  scanf("%s", yuyushiki);
  return yuyushiki;
}

// head

int n;

vi G[100010];
int sz[100010];

vi v;

pi dfs1(int x, int p) {
  pi res = pi(0, x);
  YYS(w, G[x]) {
    if (w != p) {
      pi ret = dfs1(w, x);
      ret.fi++;
      chmax(res, ret);
    }
  }
  return res;
}

int dfs2(int x, int p) {
  sz[x] = 1;
  YYS(w, G[x]) {
    if (w != p) {
      sz[x] += dfs2(w, x);
    }
  }
  return sz[x];
}

void dfs3(int x, int p) {
  int chs = -1;
  int cnt = 0;
  YYS(w, G[x]) {
    if (w != p) {
      if (sz[w] != 1) {
        if (chs == -1) {
          chs = w;
        } else {
          puts("-1");
          exit(0);
        }
      } else {
        cnt++;
      }
    }
  }
  v.pb(cnt);
  if (chs != -1) {
    dfs3(chs, x);
  }
}

int main() {

  n = in();
  REP(i, n - 1) {
    int a = in() - 1;
    int b = in() - 1;
    G[a].pb(b);
    G[b].pb(a);
  }

  int x = dfs1(0, -1).se;

  dfs2(x, -1);

  v.clear();
  dfs3(x, -1);
  v.back()--;
  v.pb(0);

  vi u = v;
  REVERSE(u);

  assert(v[0] == 0);
  assert(u[0] == 0);

  v = min(v, u);

  vi ans(0);
  int cnt = 1;
  YYS(w, v) {
    int cur = cnt++;
    REP(i, w) { ans.pb(cnt++); }
    ans.pb(cur);
  }

  // assert(SZ(ans) == n);

  REP(i, n) {
    if (i != 0) {
      printf(" ");
    }
    printf("%d", ans[i]);
  }
  printf("\n");

  return 0;
}