Problem p02908 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#include <queue>
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define REP1(u) for (int i = hd[u], v, c; v = e[i].v, c = e[i].c, i; i = e[i].n)
#define REP2(u)                                                                \
  for (int &i = cr[u], v, c; v = e[i].v, c = e[i].c, i; i = e[i].n)
using namespace std;
const int N = 200200, INF = 1000010000;
int n, x, y, ans, S, T, fl = 1;
int a[N], b[N], ca[N], cb[N], hd[N], cr[N], dep[N];
queue<int> q;
struct edge {
  int n, v, c;
} e[N];
void add_(int u, int v, int c) {
  e[++fl] = (edge){hd[u], v, c};
  hd[u] = fl;
}
void adde(int u, int v, int c) {
  add_(u, v, c);
  add_(v, u, 0);
}
bool bfs() {
  FOR(i, S, T) dep[i] = -1, cr[i] = hd[i];
  q.push(S);
  dep[S] = 0;
  while (q.size()) {
    int u = q.front();
    q.pop();
    REP1(u) if (c && dep[v] < 0) dep[v] = dep[u] + 1, q.push(v);
  }
  return ~dep[T];
}
int dfs(int u, int E) {
  if (u == T)
    return E;
  int F = 0;
  REP2(u) if (c && dep[v] == dep[u] + 1) {
    int t = dfs(v, min(c, E));
    F += t;
    e[i ^ 1].c += t;
    E -= t;
    e[i ^ 0].c -= t;
    if (!E)
      break;
  }
  return F;
}
int main() {
  // freopen("perm.in","r",stdin);
  // freopen("perm.out","w",stdout);
  scanf("%d", &n);
  S = T = 1;
  FOR(i, 1, n) scanf("%d", &a[i]);
  FOR(i, 1, n) scanf("%d", &b[i]);
  FOR(i, 1, n) if (!ca[i]) for (x = a[i], ++T; ca[x] = T, x != i; x = a[x]);
  FOR(i, 1, n) if (!cb[i]) for (x = b[i], ++T; cb[x] = T, x != i; x = b[x]);
  ++T;
  FOR(i, 1, n) {
    x = ca[i];
    y = cb[i];
    if (a[i] == b[i]) {
      if (i == a[i])
        ans++;
      else
        adde(x, y, 1), e[fl].c = 1;
    } else {
      if (i != a[i] && i != b[i])
        adde(y, x, 1);
      if (i == a[i])
        adde(y, T, 1);
      if (i == b[i])
        adde(S, x, 1);
    }
  }
  // FOR(u,1,T){cout<<u<<':';REP(u) cout<<v<<'-'<<c<<' ';cout<<'\n';}
  while (bfs())
    ans += dfs(S, INF);
  cout << n - ans << '\n';
  // FOR(i,1,n) cout<<(~dep[ca[i]]?a[i]:i)<<' ';cout<<'\n';
  // FOR(i,1,n) cout<<(~dep[cb[i]]?i:b[i])<<' ';cout<<'\n';
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <queue>
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define REP1(u) for (int i = hd[u], v, c; v = e[i].v, c = e[i].c, i; i = e[i].n)
#define REP2(u)                                                                \
  for (int &i = cr[u], v, c; v = e[i].v, c = e[i].c, i; i = e[i].n)
using namespace std;
const int N = 200200, INF = 1000010000;
int n, x, y, ans, S, T, fl = 1;
int a[N], b[N], ca[N], cb[N], hd[N], cr[N], dep[N];
queue<int> q;
struct edge {
  int n, v, c;
} e[N];
void add_(int u, int v, int c) {
  e[++fl] = (edge){hd[u], v, c};
  hd[u] = fl;
}
void adde(int u, int v, int c) {
  add_(u, v, c);
  add_(v, u, 0);
}
bool bfs() {
  FOR(i, S, T) dep[i] = -1, cr[i] = hd[i];
  q.push(S);
  dep[S] = 0;
  while (q.size()) {
    int u = q.front();
    q.pop();
    REP1(u) if (c && dep[v] < 0) dep[v] = dep[u] + 1, q.push(v);
  }
  return ~dep[T];
}
int dfs(int u, int E) {
  if (u == T)
    return E;
  int F = 0;
  REP2(u) if (c && dep[v] == dep[u] + 1) {
    int t = dfs(v, min(c, E));
    F += t;
    e[i ^ 1].c += t;
    E -= t;
    e[i ^ 0].c -= t;
    if (!E)
      break;
  }
  return F;
}
int main() {
  // freopen("perm.in","r",stdin);
  // freopen("perm.out","w",stdout);
  scanf("%d", &n);
  S = T = 1;
  FOR(i, 1, n) scanf("%d", &a[i]), a[i]++;
  FOR(i, 1, n) scanf("%d", &b[i]), b[i]++;
  FOR(i, 1, n) if (!ca[i]) for (x = a[i], ++T; ca[x] = T, x != i; x = a[x]);
  FOR(i, 1, n) if (!cb[i]) for (x = b[i], ++T; cb[x] = T, x != i; x = b[x]);
  ++T;
  FOR(i, 1, n) {
    x = ca[i];
    y = cb[i];
    if (a[i] == b[i]) {
      if (i == a[i])
        ans++;
      else
        adde(x, y, 1), e[fl].c = 1;
    } else {
      if (i != a[i] && i != b[i])
        adde(y, x, 1);
      if (i == a[i])
        adde(y, T, 1);
      if (i == b[i])
        adde(S, x, 1);
    }
  }
  // FOR(u,1,T){cout<<u<<':';REP(u) cout<<v<<'-'<<c<<' ';cout<<'\n';}
  while (bfs())
    ans += dfs(S, INF);
  cout << n - ans << '\n';
  // FOR(i,1,n) cout<<(~dep[ca[i]]?a[i]:i)<<' ';cout<<'\n';
  // FOR(i,1,n) cout<<(~dep[cb[i]]?i:b[i])<<' ';cout<<'\n';
}