Problem p03335 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

namespace TYC {
typedef long long ll;
typedef pair<int, int> pii;
const int N = 2005;

int n, m, tot, fa[N], dep[N], C[N][2];
vector<int> E[N], Tree[N], Q, T, R;
struct path {
  int a, b, type;
  vector<pii> to;
  path() {}
  path(const int _a, const int _b) : a(_a), b(_b), type(-1) { to.clear(); }
  int ano(const int x) { return a == x ? b : a; }
} P[N * N];

inline int read() {
  int x = 0, f = 0, ch = getchar();
  while (!isdigit(ch))
    f |= (ch == '-'), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}

void del(const int u, const int v) {
  E[u].erase(find(E[u].begin(), E[u].end(), v));
  E[v].erase(find(E[v].begin(), E[v].end(), u));
}

void dfs(const int u, const int f) {
  dep[u] = dep[fa[u] = f] + 1;
  for (int v : E[u])
    if (v != f)
      dfs(v, u);
}

inline int LCA(int u, int v) {
  while (u != v) {
    if (dep[u] < dep[v])
      swap(u, v);
    u = fa[u];
  }
  return u;
}

int solve(const int u) {
  T = Q;
  Q.clear();
  R.clear();
  for (auto p : T)
    if (P[p].a != u && P[p].b != u)
      Q.push_back(p);
    else if (P[p].a == u && P[p].b == u)
      P[p].type = 0;
    else
      R.push_back(p);

  int ans = min(int(R.size()), 2), f = E[u][0];
  dfs(u, 0);
  while (int(R.size()) >= 2) {
    int sz = int(R.size());
    int x = R[sz - 1], y = R[sz - 2];
    R.resize(sz - 2);
    int a = P[x].ano(u), b = P[y].ano(u);
    P[++tot] = path(a, b);
    P[tot].to.push_back({x, P[x].a != a});
    P[tot].to.push_back({y, P[y].b != b});
    Q.push_back(tot);
  }
  for (auto p : R) {
    P[++tot] = P[p].a == u ? path(f, P[p].b) : path(P[p].a, f);
    P[tot].to.push_back({p, 0});
    Q.push_back(tot);
  }
  del(u, f);
  return ans;
}

void pushdown(const int i) {
  for (auto p : P[i].to) {
    P[p.first].type = P[i].type ^ p.second;
    pushdown(p.first);
  }
}

int calc() {
  for (int i = 1; i <= n; i++)
    E[i] = Tree[i];
  dfs(1, 0);
  for (int i = 1; i <= m; i++) {
    int u = P[i].a, v = P[i].b;
    if (P[i].type)
      swap(u, v);
    int lca = LCA(u, v);
    while (u != lca)
      C[u][0] = 1, u = fa[u];
    while (v != lca)
      C[v][1] = 1, v = fa[v];
  }
  int sum = 0;
  for (int i = 1; i <= n; i++)
    sum += C[i][0] + C[i][1];
  return sum;
}

void work() {
  n = read(), m = read();
  for (int i = 1; i < n; i++) {
    int u = read(), v = read();
    E[u].push_back(v);
    E[v].push_back(u);
  }
  for (int i = 1; i <= n; i++)
    Tree[i] = E[i];
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read();
    P[++tot] = path(u, v);
    Q.push_back(tot);
  }
  int ans = 0;
  for (int k = 1; k < n; k++) {
    int v;
    for (v = 1; v <= n; v++)
      if (int(E[v].size()) == 1)
        break;
    ans += solve(v);
  }
  int d = 0;
  for (auto p : Q)
    P[p].type = d ^= 1;
  R.clear();
  for (int i = 1; i <= tot; i++)
    if (P[i].type != -1)
      R.push_back(i);
  for (auto p : R)
    pushdown(p);
  printf("%d\n", calc());
  for (int i = 1; i <= m; i++)
    if (!P[i].type)
      printf("%d %d\n", P[i].a, P[i].b);
    else
      printf("%d %d\n", P[i].b, P[i].a);
}
} // namespace TYC

int main() {
  freopen("3968.in", "r", stdin);
  freopen("3968.out", "w", stdout);
  TYC::work();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

namespace TYC {
typedef long long ll;
typedef pair<int, int> pii;
const int N = 2005;

int n, m, tot, fa[N], dep[N], C[N][2];
vector<int> E[N], Tree[N], Q, T, R;
struct path {
  int a, b, type;
  vector<pii> to;
  path() {}
  path(const int _a, const int _b) : a(_a), b(_b), type(-1) { to.clear(); }
  int ano(const int x) { return a == x ? b : a; }
} P[N * N];

inline int read() {
  int x = 0, f = 0, ch = getchar();
  while (!isdigit(ch))
    f |= (ch == '-'), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}

void del(const int u, const int v) {
  E[u].erase(find(E[u].begin(), E[u].end(), v));
  E[v].erase(find(E[v].begin(), E[v].end(), u));
}

void dfs(const int u, const int f) {
  dep[u] = dep[fa[u] = f] + 1;
  for (int v : E[u])
    if (v != f)
      dfs(v, u);
}

inline int LCA(int u, int v) {
  while (u != v) {
    if (dep[u] < dep[v])
      swap(u, v);
    u = fa[u];
  }
  return u;
}

int solve(const int u) {
  T = Q;
  Q.clear();
  R.clear();
  for (auto p : T)
    if (P[p].a != u && P[p].b != u)
      Q.push_back(p);
    else if (P[p].a == u && P[p].b == u)
      P[p].type = 0;
    else
      R.push_back(p);

  int ans = min(int(R.size()), 2), f = E[u][0];
  dfs(u, 0);
  while (int(R.size()) >= 2) {
    int sz = int(R.size());
    int x = R[sz - 1], y = R[sz - 2];
    R.resize(sz - 2);
    int a = P[x].ano(u), b = P[y].ano(u);
    P[++tot] = path(a, b);
    P[tot].to.push_back({x, P[x].a != a});
    P[tot].to.push_back({y, P[y].b != b});
    Q.push_back(tot);
  }
  for (auto p : R) {
    P[++tot] = P[p].a == u ? path(f, P[p].b) : path(P[p].a, f);
    P[tot].to.push_back({p, 0});
    Q.push_back(tot);
  }
  del(u, f);
  return ans;
}

void pushdown(const int i) {
  for (auto p : P[i].to) {
    P[p.first].type = P[i].type ^ p.second;
    pushdown(p.first);
  }
}

int calc() {
  for (int i = 1; i <= n; i++)
    E[i] = Tree[i];
  dfs(1, 0);
  for (int i = 1; i <= m; i++) {
    int u = P[i].a, v = P[i].b;
    if (P[i].type)
      swap(u, v);
    int lca = LCA(u, v);
    while (u != lca)
      C[u][0] = 1, u = fa[u];
    while (v != lca)
      C[v][1] = 1, v = fa[v];
  }
  int sum = 0;
  for (int i = 1; i <= n; i++)
    sum += C[i][0] + C[i][1];
  return sum;
}

void work() {
  n = read(), m = read();
  for (int i = 1; i < n; i++) {
    int u = read(), v = read();
    E[u].push_back(v);
    E[v].push_back(u);
  }
  for (int i = 1; i <= n; i++)
    Tree[i] = E[i];
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read();
    P[++tot] = path(u, v);
    Q.push_back(tot);
  }
  int ans = 0;
  for (int k = 1; k < n; k++) {
    int v;
    for (v = 1; v <= n; v++)
      if (int(E[v].size()) == 1)
        break;
    ans += solve(v);
  }
  int d = 0;
  for (auto p : Q)
    P[p].type = d ^= 1;
  R.clear();
  for (int i = 1; i <= tot; i++)
    if (P[i].type != -1)
      R.push_back(i);
  for (auto p : R)
    pushdown(p);
  printf("%d\n", calc());
  for (int i = 1; i <= m; i++)
    if (!P[i].type)
      printf("%d %d\n", P[i].a, P[i].b);
    else
      printf("%d %d\n", P[i].b, P[i].a);
}
} // namespace TYC

int main() {
  TYC::work();
  return 0;
}