Problem p03259 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tr1/unordered_map>
#include <vector>

typedef long long LL;
using namespace std;
#define mp make_pair
#define pb push_back
#define x first
#define y second

void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

int read() {
  int x = 0, f = 1;
  int c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = 0;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x * 10) + (c ^ 48);
    c = getchar();
  }
  return f ? x : -x;
}

#define map MAP
const int maxn = 55, maxm = maxn << 1;
int vis[maxn], anc[maxn], map[maxn][maxn], anc1[maxn];
vector<int> G[maxn], F[maxn];
pair<int, int> E[maxn];

void dfs_same(int u, int fa, int fg) {
  vis[u] = fg;
  anc[u] = fa;
  for (auto x : G[u]) {
    if (x == fa)
      continue;
    if (map[u][x])
      dfs_same(x, u, fg);
    else
      dfs_same(x, u, 0);
  }
}

void dfs_same1(int u, int fa) {
  anc1[u] = fa;
  for (auto x : F[u]) {
    if (x == fa)
      continue;
    if (map[u][x])
      dfs_same1(x, u);
    else
      dfs_same1(x, u);
  }
}

namespace tp {
int to[maxm], nxt[maxm], head[maxn], e, deg[maxn];

void add(int x, int y) {
  to[++e] = y;
  nxt[e] = head[x];
  head[x] = e;
  deg[y]++;
}

void init(int n) {
  for (int i = 1; i <= n; ++i)
    deg[i] = 0;
  e = 0;
  for (int i = 1; i <= n; ++i)
    head[i] = 0;
}

int acp(int n) {
  queue<int> Q;
  for (int i = 1; i <= n; ++i)
    if (!deg[i])
      Q.push(i);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    --n;
    for (int i = head[u]; i; i = nxt[i]) {
      int v = to[i];
      if ((--deg[v]) == 0)
        Q.push(v);
    }
  }

  return !n;
}
} // namespace tp

int main() {

  int T = read();
  while (T--) {
    // init(); //map, G

    int n = read();
    for (int i = 1; i < n; ++i) {
      int x = read(), y = read();
      G[x].pb(y);
      G[y].pb(x);
    }
    for (int i = 1; i < n; ++i) {
      int x = read(), y = read();
      map[x][y] = 1;
      map[y][x] = 1;
      E[i] = mp(x, y);
      F[x].pb(y);
      F[y].pb(x);
    }

    int ans = 0x3f3f3f3f;
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j)
        vis[j] = 0;
      dfs_same(i, 0, 1);
      dfs_same1(i, 0);

      tp::init(n);
      int tmp = n;
      for (int j = 1; j <= n; ++j) {
        tmp -= vis[j];
        if (!vis[j] && !vis[anc[j]])
          tp::add(j, anc[j]);
      }
      for (int j = 1; j < n; ++j) {
        int x = E[j].x, y = E[j].y;
        if (!vis[x] && !vis[y]) {
          if (anc1[x] == y)
            tp::add(y, x);
          else
            tp::add(x, y);
        }
      }

      if (tp::acp(n))
        ans = min(ans, tmp);
    }

    if (ans == 0x3f3f3f3f) {
      for (int i = 1; i <= n; ++i)
        if (G[i].size() == 1)
          for (int j = 1; j <= n; ++j)
            if (i != j) {
              int fi = G[i][0];
              if (fi == j)
                continue;
              for (int k = 0; k < G[fi].size(); ++k)
                if (G[fi][k] == i) {
                  G[fi].erase(G[fi].begin() + k);
                  break;
                }
              G[i][0] = j;
              G[j].pb(i);

              for (int u = 1; u <= n; ++u)
                vis[u] = 0;

              /*
              for(int u = 1; u <= n; ++u)
              {
                      for(auto x : G[u]) cout << x << ' ';
                      cout << endl;
              }*/

              dfs_same(i, 0, 1);
              dfs_same1(i, 0);
              tp::init(n);
              for (int u = 1; u <= n; ++u)
                if (!vis[u] && !vis[anc[u]])
                  tp::add(u, anc[u]);
              for (int u = 1; u < n; ++u) {
                int x = E[u].x, y = E[u].y;
                if (!vis[x] && !vis[y]) {
                  if (anc1[x] == y)
                    tp::add(y, x);
                  else
                    tp::add(x, y);
                }
              }
              if (tp::acp(n)) {
                ans = n;
                goto nxt;
              }

              G[j].pop_back();
              G[i][0] = fi;
              G[fi].pb(i);
            }
    }

  nxt:;
    if (ans == 0x3f3f3f3f)
      puts("-1");
    else
      cout << ans << endl;

    for (int i = 1; i <= n; ++i)
      G[i].clear();
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
        map[i][j] = 0;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tr1/unordered_map>
#include <vector>

typedef long long LL;
using namespace std;
#define mp make_pair
#define pb push_back
#define x first
#define y second

void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

int read() {
  int x = 0, f = 1;
  int c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = 0;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x * 10) + (c ^ 48);
    c = getchar();
  }
  return f ? x : -x;
}

#define map MAP
const int maxn = 55, maxm = maxn << 1;
int vis[maxn], anc[maxn], map[maxn][maxn], anc1[maxn];
vector<int> G[maxn], F[maxn];
pair<int, int> E[maxn];

void dfs_same(int u, int fa, int fg) {
  vis[u] = fg;
  anc[u] = fa;
  for (auto x : G[u]) {
    if (x == fa)
      continue;
    if (map[u][x])
      dfs_same(x, u, fg);
    else
      dfs_same(x, u, 0);
  }
}

void dfs_same1(int u, int fa) {
  anc1[u] = fa;
  for (auto x : F[u]) {
    if (x == fa)
      continue;
    if (map[u][x])
      dfs_same1(x, u);
    else
      dfs_same1(x, u);
  }
}

namespace tp {
int to[maxm], nxt[maxm], head[maxn], e, deg[maxn];

void add(int x, int y) {
  to[++e] = y;
  nxt[e] = head[x];
  head[x] = e;
  deg[y]++;
}

void init(int n) {
  for (int i = 1; i <= n; ++i)
    deg[i] = 0;
  e = 0;
  for (int i = 1; i <= n; ++i)
    head[i] = 0;
}

int acp(int n) {
  queue<int> Q;
  for (int i = 1; i <= n; ++i)
    if (!deg[i])
      Q.push(i);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    --n;
    for (int i = head[u]; i; i = nxt[i]) {
      int v = to[i];
      if ((--deg[v]) == 0)
        Q.push(v);
    }
  }

  return !n;
}
} // namespace tp

int main() {

  int T = read();
  while (T--) {
    // init(); //map, G

    int n = read();
    for (int i = 1; i < n; ++i) {
      int x = read(), y = read();
      G[x].pb(y);
      G[y].pb(x);
    }
    for (int i = 1; i < n; ++i) {
      int x = read(), y = read();
      map[x][y] = 1;
      map[y][x] = 1;
      E[i] = mp(x, y);
      F[x].pb(y);
      F[y].pb(x);
    }

    int ans = 0x3f3f3f3f;
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j)
        vis[j] = 0;
      dfs_same(i, 0, 1);
      dfs_same1(i, 0);

      tp::init(n);
      int tmp = n;
      for (int j = 1; j <= n; ++j) {
        tmp -= vis[j];
        if (!vis[j] && !vis[anc[j]])
          tp::add(j, anc[j]);
      }
      for (int j = 1; j < n; ++j) {
        int x = E[j].x, y = E[j].y;
        if (!vis[x] && !vis[y]) {
          if (anc1[x] == y)
            tp::add(y, x);
          else
            tp::add(x, y);
        }
      }

      if (tp::acp(n))
        ans = min(ans, tmp);
    }

    if (ans == 0x3f3f3f3f) {
      for (int i = 1; i <= n; ++i)
        if (G[i].size() == 1)
          for (int j = 1; j <= n; ++j)
            if (i != j) {
              int fi = G[i][0];
              if (fi == j)
                continue;
              for (int k = 0; k < G[fi].size(); ++k)
                if (G[fi][k] == i) {
                  G[fi].erase(G[fi].begin() + k);
                  break;
                }
              G[i][0] = j;
              G[j].pb(i);

              for (int u = 1; u <= n; ++u)
                vis[u] = 0;

              /*
              for(int u = 1; u <= n; ++u)
              {
                      for(auto x : G[u]) cout << x << ' ';
                      cout << endl;
              }*/

              dfs_same(i, 0, 1);
              dfs_same1(i, 0);
              tp::init(n);
              for (int u = 1; u <= n; ++u)
                if (!vis[u] && !vis[anc[u]])
                  tp::add(u, anc[u]);
              for (int u = 1; u < n; ++u) {
                int x = E[u].x, y = E[u].y;
                if (!vis[x] && !vis[y]) {
                  if (anc1[x] == y)
                    tp::add(y, x);
                  else
                    tp::add(x, y);
                }
              }
              if (tp::acp(n)) {
                ans = n;
                goto nxt;
              }

              G[j].pop_back();
              G[i][0] = fi;
              G[fi].pb(i);
            }
    }

  nxt:;
    if (ans == 0x3f3f3f3f)
      puts("-1");
    else
      cout << ans << endl;

    for (int i = 1; i <= n; ++i)
      G[i].clear(), F[i].clear();
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
        map[i][j] = 0;
  }

  return 0;
}