Problem p03259 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define Fail puts("NO"), exit(0);
#define eps 1e-10
#define maxn 55
#define maxm 100010
#define inf 1000000007
#define mod 1000000007
#define pi acos(-1)
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef bitset<maxn> bit;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch - '0' < 0 || ch - '0' > 9) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch - '0' >= 0 && ch - '0' <= 9) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int T;
int n;
int a[maxn][maxn], fa_a[maxn], fa_b[maxn], deg[maxn];
struct Graph {
  int head[maxn], nxt[maxn * 2], to[maxn * 2], tot;
  void add(int u, int v) {
    tot++;
    nxt[tot] = head[u];
    head[u] = tot;
    to[tot] = v;
  }
  void dfs(int x, int las, int *fa) {
    for (int i = head[x]; i; i = nxt[i]) {
      if (to[i] == las)
        continue;
      fa[to[i]] = x;
      dfs(to[i], x, fa);
    }
  }
  void init() {
    memset(head, 0, sizeof(head));
    tot = 0;
  }
} A, B;
int book[maxn];
int degx[maxn];
int head[maxn], nxt[maxn], to[maxn], tot;
void add(int u, int v) {
  tot++;
  nxt[tot] = head[u];
  head[u] = tot;
  to[tot] = v;
}
queue<int> q;
bool check() {
  for (int i = 1; i <= n; i++)
    if (!degx[i] && book[i])
      q.push(i);
  while (q.size()) {
    int now = q.front();
    q.pop();
    for (int i = head[now]; i; i = nxt[i]) {
      degx[to[i]]--;
      if (!degx[to[i]])
        q.push(to[i]);
    }
  }
  for (int i = 1; i <= n; i++)
    if (book[i] && degx[i])
      return false;
  return true;
}
int solve(int rt, int x) {
  memset(degx, 0, sizeof(degx));
  tot = 0;
  memset(head, 0, sizeof(head));
  A.dfs(x, 0, fa_a);
  fa_a[x] = rt;
  fa_a[rt] = 0;
  B.dfs(rt, 0, fa_b);
  fa_b[rt] = 0;
  int res = 0;
  for (int i = 1; i <= n; i++)
    book[i] = (fa_a[i] != fa_b[i]), res += book[i];
  for (int i = 1; i <= n; i++) {
    if (i == rt)
      continue;
    if (!book[i] && book[fa_a[i]])
      return inf;
    if (book[i] && book[fa_a[i]])
      add(i, fa_a[i]), degx[fa_a[i]]++;
    if (book[i] && book[fa_b[i]])
      add(fa_b[i], i), degx[i]++;
  }
  if (check())
    return res;
  return inf;
}
int main() {
  T = read();
  while (T--) {
    A.init();
    B.init();
    memset(a, 0, sizeof(a));
    memset(deg, 0, sizeof(deg));
    n = read();
    int ans = inf;
    for (int i = 1; i < n; i++) {
      int x = read(), y = read();
      A.add(x, y);
      A.add(y, x);
      deg[x]++;
      deg[y]++;
      a[x][y] = a[y][x] = 1;
    }
    for (int i = 1; i < n; i++) {
      int x = read(), y = read();
      B.add(x, y);
      B.add(y, x);
    }
    for (int i = 1; i <= n; i++) {
      if (deg[i] != 1)
        continue;
      for (int j = 1; j <= n; j++) {
        if (i == j)
          continue;
        ans = min(ans, solve(i, j) + (!a[i][j]));
      }
    }
    if (ans == inf)
      puts("-1");
    else
      printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define Fail puts("NO"), exit(0);
#define eps 1e-10
#define maxn 55
#define maxm 100010
#define inf 1000000007
#define mod 1000000007
#define pi acos(-1)
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef bitset<maxn> bit;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch - '0' < 0 || ch - '0' > 9) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch - '0' >= 0 && ch - '0' <= 9) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int T;
int n;
int a[maxn][maxn], fa_a[maxn], fa_b[maxn], deg[maxn];
struct Graph {
  int head[maxn], nxt[maxn * 2], to[maxn * 2], tot;
  void add(int u, int v) {
    tot++;
    nxt[tot] = head[u];
    head[u] = tot;
    to[tot] = v;
  }
  void dfs(int x, int las, int *fa) {
    for (int i = head[x]; i; i = nxt[i]) {
      if (to[i] == las)
        continue;
      fa[to[i]] = x;
      dfs(to[i], x, fa);
    }
  }
  void init() {
    memset(head, 0, sizeof(head));
    tot = 0;
  }
} A, B;
int book[maxn];
int degx[maxn];
int head[maxn], nxt[maxn * 2], to[maxn * 2], tot;
void add(int u, int v) {
  tot++;
  nxt[tot] = head[u];
  head[u] = tot;
  to[tot] = v;
}
queue<int> q;
bool check() {
  for (int i = 1; i <= n; i++)
    if (!degx[i] && book[i])
      q.push(i);
  while (q.size()) {
    int now = q.front();
    q.pop();
    for (int i = head[now]; i; i = nxt[i]) {
      degx[to[i]]--;
      if (!degx[to[i]])
        q.push(to[i]);
    }
  }
  for (int i = 1; i <= n; i++)
    if (book[i] && degx[i])
      return false;
  return true;
}
int solve(int rt, int x) {
  memset(degx, 0, sizeof(degx));
  tot = 0;
  memset(head, 0, sizeof(head));
  A.dfs(x, 0, fa_a);
  fa_a[x] = rt;
  fa_a[rt] = 0;
  B.dfs(rt, 0, fa_b);
  fa_b[rt] = 0;
  int res = 0;
  for (int i = 1; i <= n; i++)
    book[i] = (fa_a[i] != fa_b[i]), res += book[i];
  for (int i = 1; i <= n; i++) {
    if (i == rt)
      continue;
    if (!book[i] && book[fa_a[i]])
      return inf;
    if (book[i] && book[fa_a[i]])
      add(i, fa_a[i]), degx[fa_a[i]]++;
    if (book[i] && book[fa_b[i]])
      add(fa_b[i], i), degx[i]++;
  }
  if (check())
    return res;
  return inf;
}
int main() {
  T = read();
  while (T--) {
    A.init();
    B.init();
    memset(a, 0, sizeof(a));
    memset(deg, 0, sizeof(deg));
    n = read();
    int ans = inf;
    for (int i = 1; i < n; i++) {
      int x = read(), y = read();
      A.add(x, y);
      A.add(y, x);
      deg[x]++;
      deg[y]++;
      a[x][y] = a[y][x] = 1;
    }
    for (int i = 1; i < n; i++) {
      int x = read(), y = read();
      B.add(x, y);
      B.add(y, x);
    }
    for (int i = 1; i <= n; i++) {
      if (deg[i] != 1)
        continue;
      for (int j = 1; j <= n; j++) {
        if (i == j)
          continue;
        ans = min(ans, solve(i, j) + (!a[i][j]));
      }
    }
    if (ans == inf)
      puts("-1");
    else
      printf("%d\n", ans);
  }
  return 0;
}