Problem p03335 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N = 10002;
int n, m, a, b, i, j, k, h[N], t[N], v[N];
int x[N], y[N], X[N], Y[N], fa[N], dep[N], val[N], ac[N];
int ans, id[N], d, l, r, cnt[2002], vis[N], col[N], oa, ob;
vector<int> e[N], w[N];
void add(int a, int b) {
  t[++k] = h[a];
  h[a] = k;
  v[k] = b;
  t[++k] = h[b];
  h[b] = k;
  v[k] = a;
}
void dfs(int i) {
  for (int j = h[i]; j; j = t[j])
    if (v[j] != fa[i]) {
      fa[v[j]] = i;
      dep[v[j]] = dep[i] + 1;
      dfs(v[j]);
    }
}
// clear x[i]==y[i];
void init() {
  scanf("%d%d", &n, &m);
  for (i = 1; i < n; i++) {
    scanf("%d%d", &a, &b);
    add(a, b);
    add(b, a);
  }
  dfs(1);
  for (i = 1; i <= m; i++) {
    // ac[i]=i;ac[i+m]=i+m;???
    scanf("%d%d", x + i, y + i);
    X[i] = x[i];
    Y[i] = y[i];
    a = x[i];
    b = y[i];
    while (a != b) {
      if (dep[a] > dep[b]) {
        val[a]++;
        a = fa[a];
      } else {
        val[b]++;
        b = fa[b];
      }
    }
  }
  for (i = 1; i <= n; i++) {
    if (val[i] > 2)
      val[i] = 2;
    ans += val[i];
  }
  printf("%d\n", ans);
  for (i = 1; i <= n; i++)
    id[i] = i;
}
bool cmp(int x, int y) { return dep[x] > dep[y]; }
void Cover(int x, int y) {
  while (x != y) {
    if (dep[x] > dep[y]) {
      cnt[x]++;
      x = fa[x];
    } else {
      cnt[y]++;
      y = fa[y];
    }
  }
}
void Adde(int a, int b, int c) {
  e[a].push_back(b);
  w[a].push_back(c);
  e[b].push_back(a);
  w[b].push_back(c);
}
void D(int i) {
  vis[i] = 1;
  for (int j = 0; j < e[i].size(); j++)
    if (!vis[e[i][j]]) {
      col[e[i][j]] = col[i] ^ w[i][j];
      D(e[i][j]);
    }
}
void work() {
  sort(id + 1, id + n + 1, cmp);
  for (i = 1; i <= n; i++) {
    k = id[i];
    if (val[k] < 2) {
      for (d = 1; d <= m; d++) {
        if (x[d] == k)
          x[d] = fa[x[d]];
        if (y[d] == k)
          y[d] = fa[y[d]];
      }
      val[k] = 0;
    } else {
      a = b = 0;
      for (d = 1; d <= m; d++) {
        if (x[d] == k || y[d] == k) {
          if (!a) {
            a = d;
            if (x[d] == k)
              oa = 1;
            else
              oa = 0;
          } else {
            b = d;
            if (x[d] == k)
              ob = 1;
            else
              ob = 0;
          }
        }
      }
      if (a == 0 || b == 0 || x[a] == 0 || y[a] == 0 || x[b] == 0 || y[b] == 0)
        break;
      Adde(a, b, oa ^ ob ^ 1);
      memset(cnt, 0, sizeof(cnt));
      Cover(x[a], y[a]);
      Cover(x[b], y[b]);
      for (d = 1; d <= n; d++)
        if (cnt[d] == 2)
          val[d] = 0;
      if (x[a] == k) {
        x[a] ^= x[b] ^ y[b];
      } else {
        y[a] ^= x[b] ^ y[b];
      }
      x[b] = y[b] = 0;
      for (d = 1; d <= m; d++) {
        if (x[d] == k)
          x[d] = fa[x[d]];
        if (y[d] == k)
          y[d] = fa[y[d]];
      }
    }
    for (d = 1; d <= m; d++)
      if (x[d] == y[d]) {
        x[d] = y[d] = 0;
      }
  }
  for (i = 1; i <= m; i++)
    if (!vis[i])
      D(i);
  for (i = 1; i <= m; i++)
    if (col[i] == 0)
      printf("%d %d\n", X[i], Y[i]);
    else
      printf("%d %d\n", Y[i], X[i]);
}
int main() {
  init();
  work();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N = 10002;
int n, m, a, b, i, j, k, h[N], t[N], v[N];
int x[N], y[N], X[N], Y[N], fa[N], dep[N], val[N], ac[N];
int ans, id[N], d, l, r, cnt[2002], vis[N], col[N], oa, ob;
vector<int> e[N], w[N];
void add(int a, int b) {
  t[++k] = h[a];
  h[a] = k;
  v[k] = b;
}
void dfs(int i) {
  for (int j = h[i]; j; j = t[j])
    if (v[j] != fa[i]) {
      fa[v[j]] = i;
      dep[v[j]] = dep[i] + 1;
      dfs(v[j]);
    }
}
// clear x[i]==y[i];
void init() {
  scanf("%d%d", &n, &m);
  for (i = 1; i < n; i++) {
    scanf("%d%d", &a, &b);
    add(a, b);
    add(b, a);
  }
  dfs(1);
  for (i = 1; i <= m; i++) {
    // ac[i]=i;ac[i+m]=i+m;???
    scanf("%d%d", x + i, y + i);
    X[i] = x[i];
    Y[i] = y[i];
    a = x[i];
    b = y[i];
    while (a != b) {
      if (dep[a] > dep[b]) {
        val[a]++;
        a = fa[a];
      } else {
        val[b]++;
        b = fa[b];
      }
    }
  }
  for (i = 1; i <= n; i++) {
    if (val[i] > 2)
      val[i] = 2;
    ans += val[i];
  }
  printf("%d\n", ans);
  for (i = 1; i <= n; i++)
    id[i] = i;
}
bool cmp(int x, int y) { return dep[x] > dep[y]; }
void Cover(int x, int y) {
  while (x != y) {
    if (dep[x] > dep[y]) {
      cnt[x]++;
      x = fa[x];
    } else {
      cnt[y]++;
      y = fa[y];
    }
  }
}
void Adde(int a, int b, int c) {
  e[a].push_back(b);
  w[a].push_back(c);
  e[b].push_back(a);
  w[b].push_back(c);
}
void D(int i) {
  vis[i] = 1;
  for (int j = 0; j < e[i].size(); j++)
    if (!vis[e[i][j]]) {
      col[e[i][j]] = col[i] ^ w[i][j];
      D(e[i][j]);
    }
}
void work() {
  sort(id + 1, id + n + 1, cmp);
  for (i = 1; i <= n; i++) {
    k = id[i];
    if (val[k] < 2) {
      for (d = 1; d <= m; d++) {
        if (x[d] == k)
          x[d] = fa[x[d]];
        if (y[d] == k)
          y[d] = fa[y[d]];
      }
      val[k] = 0;
    } else {
      a = b = 0;
      for (d = 1; d <= m; d++) {
        if (x[d] == k || y[d] == k) {
          if (!a) {
            a = d;
            if (x[d] == k)
              oa = 1;
            else
              oa = 0;
          } else {
            b = d;
            if (x[d] == k)
              ob = 1;
            else
              ob = 0;
          }
        }
      }
      if (a == 0 || b == 0 || x[a] == 0 || y[a] == 0 || x[b] == 0 || y[b] == 0)
        break;
      Adde(a, b, oa ^ ob ^ 1);
      memset(cnt, 0, sizeof(cnt));
      Cover(x[a], y[a]);
      Cover(x[b], y[b]);
      for (d = 1; d <= n; d++)
        if (cnt[d] == 2)
          val[d] = 0;
      if (x[a] == k) {
        x[a] ^= x[b] ^ y[b];
      } else {
        y[a] ^= x[b] ^ y[b];
      }
      x[b] = y[b] = 0;
      for (d = 1; d <= m; d++) {
        if (x[d] == k)
          x[d] = fa[x[d]];
        if (y[d] == k)
          y[d] = fa[y[d]];
      }
    }
    for (d = 1; d <= m; d++)
      if (x[d] == y[d]) {
        x[d] = y[d] = 0;
      }
  }
  for (i = 1; i <= m; i++)
    if (!vis[i])
      D(i);
  for (i = 1; i <= m; i++)
    if (col[i] == 0)
      printf("%d %d\n", X[i], Y[i]);
    else
      printf("%d %d\n", Y[i], X[i]);
}
int main() {
  init();
  work();
  return 0;
}