1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector>
using namespace std;
const int maxn = 5e+4 + 5; const int maxm = 1e+5 + 5;
int n, a[maxn], b[maxn], f[maxn], siz[maxn]; int dis[maxn], cnt[maxn], dt[maxn]; int bkta[maxm], bktb[maxm]; vector< pair<int, int> > vec;
int find(int x) { if (x == f[x]) return x; int rt = find(f[x]); dis[x] = (dis[x] + dis[f[x]]) % 2; return f[x] = rt; } int link(int x, int y, int d) { int fx = find(x), fy = find(y); if (fx == fy) { int del = (dis[x] + dis[y]) % 2; return (d + del) % 2; } else { int equ = (d - dis[x] + dis[y] + 2) % 2; f[fx] = fy; dis[fx] = equ; siz[fy] += siz[fx]; return 0; } }
int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) f[i] = i, siz[i] = 1; for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); if (bkta[a[i]]) link(bkta[a[i]], i, 1); else bkta[a[i]] = i; } for (int i = 1; i <= n; ++i) { scanf("%d", &b[i]); if (bktb[b[i]]) { link(bktb[b[i]], i, 1); } else { if (bkta[b[i]]) link(bkta[b[i]], i, 0); else bktb[b[i]] = i; } } int ans = 0; for (int i = 1; i <= n; ++i) { int fx = find(i); if (!dis[i]) ++cnt[fx]; } for (int i = 1; i <= n; ++i) { int fx = find(i); if (!dt[fx]) { ans += min(cnt[fx], siz[fx] - cnt[fx]); dt[fx] = 1; } } printf("%d\n", ans); return 0; }
|