Problem p03384 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;

int N;
vector<int> G[200000];
bool used[200000];
int cnt[200000];
int idx1[200001];
int idx2[200001];
vector<int> vs;

void dfs(int v) {
  used[v] = true;
  vs.push_back(v);
  for (int to : G[v]) {
    if (used[to])
      continue;
    if (cnt[to] == 0)
      continue;
    dfs(to);
  }
}

void print(vector<int> ans) {
  for (int i : ans)
    cout << i << ' ';
  cout << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
    cnt[u]++;
    cnt[v]++;
  }
  for (int i = 0; i < N; i++) {
    if (G[i].size() == 1) {
      int to = G[i][0];
      cnt[to]--;
      cnt[i]--;
    }
  }
  int v;
  for (int i = 0; i < N; i++) {
    if (cnt[i] >= 3) {
      cout << -1 << endl;
      return 0;
    }
    if (cnt[i] == 1)
      v = i;
  }
  bool f = false;
  for (int i = 0; i < N; i++) {
    if (cnt[i] == 1) {
      dfs(i);
      f = true;
      break;
    }
  }
  if (!f) {
    return -1;
  }
  int cur = 1;
  vector<int> ans1, ans2;
  ans1.push_back(1);
  for (int i = 0; i < vs.size(); i++) {
    int v = vs[i];
    int sz = G[v].size() - 2;
    for (int i = 1; i <= sz; i++)
      ans1.push_back(cur + i + 1);
    ans1.push_back(cur + 1);
    cur += (sz + 1);
  }
  ans1.push_back(N);
  cur = 1;
  ans2.push_back(1);
  for (int i = vs.size() - 1; i >= 0; i--) {
    int v = vs[i];
    int sz = G[v].size() - 2;
    for (int i = 1; i <= sz; i++)
      ans2.push_back(cur + i + 1);
    ans2.push_back(cur + 1);
    cur += (sz + 1);
  }
  ans2.push_back(N);

  for (int i = 0; i < N; i++) {
    if (ans1[i] < ans2[i]) {
      print(ans1);
      return 0;
    }
    if (ans2[i] < ans1[i]) {
      print(ans2);
      return 0;
    }
  }
  print(ans1);
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;

int N;
vector<int> G[200000];
bool used[200000];
int cnt[200000];
int idx1[200001];
int idx2[200001];
vector<int> vs;

void dfs(int v) {
  used[v] = true;
  vs.push_back(v);
  for (int to : G[v]) {
    if (used[to])
      continue;
    if (cnt[to] == 0)
      continue;
    dfs(to);
  }
}

void print(vector<int> ans) {
  for (int i : ans)
    cout << i << ' ';
  cout << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
    cnt[u]++;
    cnt[v]++;
  }
  for (int i = 0; i < N; i++) {
    if (G[i].size() == 1) {
      int to = G[i][0];
      cnt[to]--;
      cnt[i]--;
    }
  }
  int v;
  for (int i = 0; i < N; i++) {
    if (cnt[i] >= 3) {
      cout << -1 << endl;
      return 0;
    }
    if (cnt[i] == 1)
      v = i;
  }
  bool f = false;
  for (int i = 0; i < N; i++) {
    if (cnt[i] == 1) {
      dfs(i);
      f = true;
      break;
    }
  }
  if (!f) {
    cout << 1 << ' ';
    for (int i = 3; i <= N - 1; i++)
      cout << i << ' ';
    cout << 2 << ' ' << N << endl;
    return 0;
  }
  int cur = 1;
  vector<int> ans1, ans2;
  ans1.push_back(1);
  for (int i = 0; i < vs.size(); i++) {
    int v = vs[i];
    int sz = G[v].size() - 2;
    for (int i = 1; i <= sz; i++)
      ans1.push_back(cur + i + 1);
    ans1.push_back(cur + 1);
    cur += (sz + 1);
  }
  ans1.push_back(N);
  cur = 1;
  ans2.push_back(1);
  for (int i = vs.size() - 1; i >= 0; i--) {
    int v = vs[i];
    int sz = G[v].size() - 2;
    for (int i = 1; i <= sz; i++)
      ans2.push_back(cur + i + 1);
    ans2.push_back(cur + 1);
    cur += (sz + 1);
  }
  ans2.push_back(N);

  for (int i = 0; i < N; i++) {
    if (ans1[i] < ans2[i]) {
      print(ans1);
      return 0;
    }
    if (ans2[i] < ans1[i]) {
      print(ans2);
      return 0;
    }
  }
  print(ans1);
}