Problem p03384 (C++)

Buggy submission (status: Runtime Error):
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
const int N = 1e5 + 5;
struct eg {
  int v, nx;
} e[N << 1];
int head[N], tot;
inline void add(R int u, R int v) { e[++tot] = {v, head[u]}, head[u] = tot; }
int deg[N], d[N], st[N], ins[N], ans[N], q[N], t, top, n;
inline void ck() {
  fp(i, 1, n) if (ans[i] != q[i]) {
    if (ans[i] > q[i])
      fp(j, 1, n) ans[j] = q[j];
    return;
  }
}
void dfs(int u, int fa) {
  st[++top] = d[u];
  go(u) if (v != fa && !ins[v]) dfs(v, u);
}
int main() {
  //	freopen("testdata.in","r",stdin);
  scanf("%d", &n);
  for (R int i = 1, u, v; i < n; ++i) {
    scanf("%d%d", &u, &v), add(u, v), add(v, u);
    ++deg[u], ++deg[v];
  }
  fp(i, 1, n) ans[i] = n + 1, d[i] = deg[i];
  fp(u, 1, n) if (deg[u] == 1) st[++top] = u, ins[u] = 1;
  fp(p, 1, top) go(st[p])-- deg[v];
  fp(i, 1, n) if (!ins[i] && deg[i] > 2) return puts("-1"), 0;
  fp(i, 1, n) if (!ins[i] && deg[i] == 1) {
    top = 1;
    dfs(i, 0);
    break;
  }
  st[1] = 0, st[++top] = 0;
  fp(i, 2, top - 1) st[i] -= 2;
  q[t = 1] = 1;
  for (R int i = 2, k = 2; i <= top; ++i) {
    q[t + st[i] + 1] = k++;
    fp(j, 1, st[i]) q[++t] = k++;
    ++t;
  }
  ck();
  reverse(st + 1, st + 1 + top);
  q[t = 1] = 1;
  for (R int i = 2, k = 2; i <= top; ++i) {
    q[t + st[i] + 1] = k++;
    fp(j, 1, st[i]) q[++t] = k++;
    ++t;
  }
  ck();
  fp(i, 1, n) printf("%d%c", ans[i], " \n"[i == n]);
  return 0;
}

Fixed submission:
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
const int N = 1e5 + 5;
struct eg {
  int v, nx;
} e[N << 1];
int head[N], tot;
inline void add(R int u, R int v) { e[++tot] = {v, head[u]}, head[u] = tot; }
int deg[N], d[N], st[N], ins[N], ans[N], q[N], t, top, n;
inline void ck() {
  fp(i, 1, n) if (ans[i] != q[i]) {
    if (ans[i] > q[i])
      fp(j, 1, n) ans[j] = q[j];
    return;
  }
}
void dfs(int u, int fa) {
  st[++top] = d[u];
  go(u) if (v != fa && !ins[v]) dfs(v, u);
}
int main() {
  //	freopen("testdata.in","r",stdin);
  scanf("%d", &n);
  for (R int i = 1, u, v; i < n; ++i) {
    scanf("%d%d", &u, &v), add(u, v), add(v, u);
    ++deg[u], ++deg[v];
  }
  fp(i, 1, n) ans[i] = n + 1, d[i] = deg[i];
  fp(u, 1, n) if (deg[u] == 1) st[++top] = u, ins[u] = 1;
  fp(p, 1, top) go(st[p])-- deg[v];
  fp(i, 1, n) if (!ins[i] && deg[i] > 2) return puts("-1"), 0;
  fp(i, 1, n) if (!ins[i] && deg[i] <= 1) {
    top = 1;
    dfs(i, 0);
    break;
  }
  st[1] = 0, st[++top] = 0;
  fp(i, 2, top - 1) st[i] -= 2;
  q[t = 1] = 1;
  for (R int i = 2, k = 2; i <= top; ++i) {
    q[t + st[i] + 1] = k++;
    fp(j, 1, st[i]) q[++t] = k++;
    ++t;
  }
  ck();
  reverse(st + 1, st + 1 + top);
  q[t = 1] = 1;
  for (R int i = 2, k = 2; i <= top; ++i) {
    q[t + st[i] + 1] = k++;
    fp(j, 1, st[i]) q[++t] = k++;
    ++t;
  }
  ck();
  fp(i, 1, n) printf("%d%c", ans[i], " \n"[i == n]);
  return 0;
}