Problem p03384 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
using ll = long long;
using vi = vector<int>;
vi e[100000];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int v, w;
    cin >> v >> w;
    v--;
    w--;
    e[v].push_back(w);
    e[w].push_back(v);
  }
  vector<bool> bicdim(n);
  for (int i = 0; i < n; i++) {
    bicdim[i] = e[i].size() > 1;
  }
  vi st;
  for (int i = 0; i < n; i++) {
    int c = 0;
    for (int nv : e[i]) {
      if (bicdim[nv])
        c++;
    }
    if (c > 2) {
      cout << -1 << endl;
      return 0;
    }
    if (c == 1 && bicdim[i]) {
      st.push_back(i);
    }
  }

  vi ans1;
  int s;
  for (int k : e[st[0]]) {
    if (!bicdim[k]) {
      s = k;
      break;
    }
  }
  int now = s;
  bicdim[s] = true;
  vector<bool> used(n, false);
  used[now] = true;
  while (true) {
    int k = ans1.size();
    int c = 0;
    for (int nv : e[now]) {
      if (!bicdim[nv])
        c++;
    }
    if (now == st[1]) {
      for (int i = k + 2; i < k + c + 1; i++) {
        ans1.push_back(i);
      }
      ans1.push_back(k + 1);
      ans1.push_back(ans1.size() + 1);
      break;
    }
    for (int i = k + 2; i < k + c + 2; i++) {
      ans1.push_back(i);
    }
    ans1.push_back(k + 1);
    for (int nv : e[now]) {
      if (bicdim[nv] && !used[nv]) {
        now = nv;
        used[nv] = true;
        break;
      }
    }
  }
  bicdim[s] = false;
  vi ans2;
  s;
  for (int k : e[st[1]]) {
    if (!bicdim[k]) {
      s = k;
      break;
    }
  }
  now = s;
  bicdim[s] = true;
  used.resize(0);
  used.resize(n, false);
  used[now] = true;
  while (true) {
    int k = ans2.size();
    int c = 0;
    for (int nv : e[now]) {
      if (!bicdim[nv])
        c++;
    }
    if (now == st[0]) {
      for (int i = k + 2; i < k + c + 1; i++) {
        ans2.push_back(i);
      }
      ans2.push_back(k + 1);
      ans2.push_back(ans2.size() + 1);
      break;
    }
    for (int i = k + 2; i < k + c + 2; i++) {
      ans2.push_back(i);
    }
    ans2.push_back(k + 1);
    for (int nv : e[now]) {
      if (bicdim[nv] && !used[nv]) {
        now = nv;
        used[nv] = true;
        break;
      }
    }
  }
  vi ans = ans1 > ans2 ? ans2 : ans1;
  for (int i = 0; i < n; i++) {
    cout << ans[i];
    if (i != n - 1) {
      cout << " ";
    }
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
using ll = long long;
using vi = vector<int>;
vi e[100000];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int v, w;
    cin >> v >> w;
    v--;
    w--;
    e[v].push_back(w);
    e[w].push_back(v);
  }
  vector<bool> bicdim(n);
  for (int i = 0; i < n; i++) {
    bicdim[i] = e[i].size() > 1;
  }
  vi st;
  for (int i = 0; i < n; i++) {
    int c = 0;
    for (int nv : e[i]) {
      if (bicdim[nv])
        c++;
    }
    if (c > 2) {
      cout << -1 << endl;
      return 0;
    }
    if (c == 1 && bicdim[i]) {
      st.push_back(i);
    }
  }
  if (st.size() == 0) {
    vi ans;
    ans.push_back(1);
    for (int i = 3; i < n; i++) {
      ans.push_back(i);
    }
    ans.push_back(2);
    ans.push_back(n);
    for (int i = 0; i < n; i++) {
      cout << ans[i];
      if (i != n - 1) {
        cout << " ";
      }
    }
    cout << endl;
    return 0;
  }

  vi ans1;
  int s;
  for (int k : e[st[0]]) {
    if (!bicdim[k]) {
      s = k;
      break;
    }
  }
  int now = s;
  bicdim[s] = true;
  vector<bool> used(n, false);
  used[now] = true;
  while (true) {
    int k = ans1.size();
    int c = 0;
    for (int nv : e[now]) {
      if (!bicdim[nv])
        c++;
    }
    if (now == st[1]) {
      for (int i = k + 2; i < k + c + 1; i++) {
        ans1.push_back(i);
      }
      ans1.push_back(k + 1);
      ans1.push_back(ans1.size() + 1);
      break;
    }
    for (int i = k + 2; i < k + c + 2; i++) {
      ans1.push_back(i);
    }
    ans1.push_back(k + 1);
    for (int nv : e[now]) {
      if (bicdim[nv] && !used[nv]) {
        now = nv;
        used[nv] = true;
        break;
      }
    }
  }
  bicdim[s] = false;
  vi ans2;
  s;
  for (int k : e[st[1]]) {
    if (!bicdim[k]) {
      s = k;
      break;
    }
  }
  now = s;
  bicdim[s] = true;
  used.resize(0);
  used.resize(n, false);
  used[now] = true;
  while (true) {
    int k = ans2.size();
    int c = 0;
    for (int nv : e[now]) {
      if (!bicdim[nv])
        c++;
    }
    if (now == st[0]) {
      for (int i = k + 2; i < k + c + 1; i++) {
        ans2.push_back(i);
      }
      ans2.push_back(k + 1);
      ans2.push_back(ans2.size() + 1);
      break;
    }
    for (int i = k + 2; i < k + c + 2; i++) {
      ans2.push_back(i);
    }
    ans2.push_back(k + 1);
    for (int nv : e[now]) {
      if (bicdim[nv] && !used[nv]) {
        now = nv;
        used[nv] = true;
        break;
      }
    }
  }
  vi ans = ans1 > ans2 ? ans2 : ans1;
  for (int i = 0; i < n; i++) {
    cout << ans[i];
    if (i != n - 1) {
      cout << " ";
    }
  }
  cout << endl;
  return 0;
}