Problem p03384 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
typedef pair<int, int> pint;
typedef long long ll;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const int mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

vector<int> v[101010], ans[2];
int used[101010];
int calc(int x) {
  if (v[x].size() == 1)
    return 0;
  int ret = 0;
  rep(i, v[x].size()) {
    if ((int)v[v[x][i]].size() != 1)
      ret++;
  }
  return ret;
}

void dfs(int x, int k, int s) {
  used[x] = true;
  if (v[x].size() == 1)
    return;
  int m = v[x].size();
  ans[s].push_back(k - m + 1);
  rep(i, m - 2) { ans[s].push_back(k - i - 1); }
  rep(i, m) {
    if (used[v[x][i]])
      continue;
    dfs(v[x][i], k - m + 1, s);
  }
}

int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  if (n <= 3) {
    rep(i, n) cout << i + 1 << " ";
    cout << endl;
    return 0;
  }
  vector<int> st;
  rep(i, n) {
    if (calc(i) >= 3) {
      cout << -1 << endl;
      return 0;
    }
    if (calc(i) == 1)
      st.push_back(i);
  }
  rep(i, 2) st.push_back(0);
  rep(j, 2) {
    ans[j].push_back(n);
    rep(i, n) used[i] = false;
    dfs(st[j], n, j);
    ans[j].push_back(1);
    reverse(ans[j].begin(), ans[j].end());
  }
  int mi = 0;
  rep(i, n) {
    if (ans[0][i] == ans[1][i])
      continue;
    if (ans[0][i] < ans[1][i])
      break;
    if (ans[0][i] > ans[1][i]) {
      mi = 1;
      break;
    }
  }

  rep(i, n) cout << ans[mi][i] << " ";
  cout << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
typedef pair<int, int> pint;
typedef long long ll;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const int mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

vector<int> v[101010], ans[2];
int used[101010];
int calc(int x) {
  if (v[x].size() == 1)
    return 0;
  int ret = 0;
  rep(i, v[x].size()) {
    if ((int)v[v[x][i]].size() != 1)
      ret++;
  }
  return ret;
}

void dfs(int x, int k, int s) {
  used[x] = true;
  if (v[x].size() == 1)
    return;
  int m = v[x].size();
  ans[s].push_back(k - m + 1);
  rep(i, m - 2) { ans[s].push_back(k - i - 1); }
  rep(i, m) {
    if (used[v[x][i]])
      continue;
    dfs(v[x][i], k - m + 1, s);
  }
}

int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  if (n <= 3) {
    rep(i, n) cout << i + 1 << " ";
    cout << endl;
    return 0;
  }
  vector<int> st;
  rep(i, n) {
    if (calc(i) >= 3) {
      cout << -1 << endl;
      return 0;
    }
    if (calc(i) == 1)
      st.push_back(i);
  }
  rep(i, 2) st.push_back(v[0][0]);
  rep(j, 2) {
    ans[j].push_back(n);
    rep(i, n) used[i] = false;
    dfs(st[j], n, j);
    ans[j].push_back(1);
    reverse(ans[j].begin(), ans[j].end());
  }
  int mi = 0;
  rep(i, n) {
    if (ans[0][i] == ans[1][i])
      continue;
    if (ans[0][i] < ans[1][i])
      break;
    if (ans[0][i] > ans[1][i]) {
      mi = 1;
      break;
    }
  }

  rep(i, n) cout << ans[mi][i] << " ";
  cout << endl;
  return 0;
}