Problem p03384 (C++)

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

using namespace std;

int n;
vector<int> v[100000];

vector<int> calc(int x) {
  int p = -1, num = 1, i;
  vector<int> w;

  while (1) {
    int nx = -1, next = num++;

    for (i = 0; i < v[x].size(); i++) {
      int y = v[x][i];

      if (y == p)
        continue;

      if (v[y].size() == 1) {
        w.push_back(num++);
      } else {
        nx = y;
      }
    }

    w.push_back(next);

    if (nx == -1) {
      swap(w[n - 2], w[n - 1]);
      break;
    } else {
      p = x;
      x = nx;
    }
  }

  return w;
}

int main() {
  int p = -1, q = -1, i, j;
  vector<int> ans1, ans2;

  scanf("%d", &n);

  for (i = 0; i < n - 1; i++) {
    int x, y;

    scanf("%d %d", &x, &y);

    x--;
    y--;

    v[x].push_back(y);
    v[y].push_back(x);
  }

  if (n == 2) {
    puts("1 2");
    return 0;
  }

  for (i = 0; i < n; i++) {
    int c = 0, z;

    if (v[i].size() == 1)
      continue;

    for (j = 0; j < v[i].size(); j++) {
      int x = v[i][j];

      if (v[x].size() == 1) {
        z = x;
      } else {
        c++;
      }
    }

    if (c > 2) {
      puts("-1");
      return 0;
    }

    if (c == 1) {
      if (p == -1) {
        p = z;
      } else {
        q = z;
      }
    }
  }

  if (q == -1)
    q = p;

  ans1 = calc(p);
  ans2 = calc(q);

  for (i = 0; i < n; i++) {
    if (ans1[i] != ans2[i]) {
      if (ans1[i] > ans2[i])
        swap(ans1, ans2);
      break;
    }
  }

  for (i = 0; i < n; i++) {
    if (i > 0)
      putchar(' ');
    printf("%d", ans1[i]);
  }
  puts("");

  return 0;
}


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

using namespace std;

int n;
vector<int> v[100000];

vector<int> calc(int x) {
  int p = -1, num = 1, i;
  vector<int> w;

  while (1) {
    int nx = -1, next = num++;

    for (i = 0; i < v[x].size(); i++) {
      int y = v[x][i];

      if (y == p)
        continue;

      if (v[y].size() == 1) {
        w.push_back(num++);
      } else {
        nx = y;
      }
    }

    w.push_back(next);

    if (nx == -1) {
      swap(w[n - 2], w[n - 1]);
      break;
    } else {
      p = x;
      x = nx;
    }
  }

  return w;
}

int main() {
  int p = -1, q = -1, i, j;
  vector<int> ans1, ans2;

  scanf("%d", &n);

  for (i = 0; i < n - 1; i++) {
    int x, y;

    scanf("%d %d", &x, &y);

    x--;
    y--;

    v[x].push_back(y);
    v[y].push_back(x);
  }

  if (n == 2) {
    puts("1 2");
    return 0;
  }

  for (i = 0; i < n; i++) {
    int c = 0, z;

    if (v[i].size() == 1)
      continue;

    for (j = 0; j < v[i].size(); j++) {
      int x = v[i][j];

      if (v[x].size() == 1) {
        z = x;
      } else {
        c++;
      }
    }

    if (c > 2) {
      puts("-1");
      return 0;
    }

    if (c <= 1) {
      if (p == -1) {
        p = z;
      } else {
        q = z;
      }
    }
  }

  if (q == -1)
    q = p;

  ans1 = calc(p);
  ans2 = calc(q);

  for (i = 0; i < n; i++) {
    if (ans1[i] != ans2[i]) {
      if (ans1[i] > ans2[i])
        swap(ans1, ans2);
      break;
    }
  }

  for (i = 0; i < n; i++) {
    if (i > 0)
      putchar(' ');
    printf("%d", ans1[i]);
  }
  puts("");

  return 0;
}