版本比较
题解:AT_agc038_f [AGC038F] Two Permutations
正文
增加 10 行删除 10 行
1223445678991011121399 行未改动1131141151161171171181191201218 行未改动13013113213313413413513613713813914014114214314314414514614749 行未改动19719819920020120120220320420520520620720820920921021121221321421521621621721821922014 行未改动**题目链接**
[atcoder](https://atcoder.jp/contests/agc038/tasks/agc038_f)
[luogu](https://www.luogu.com.cn/problem/AT_agc038_f)
# 题目简介
给定 $P, Q$ 两个排列,目标求 $A, B$ 两个排列满足 给定 $P, Q$ 两个排列,目标求 $A, B$ 两个排列满足
$A_i \in \{i, P_i\}$, $B_i \in \{i, Q_i\}$ **最大化** $A_i \neq B_i$ 的数量。
数据范围:$n \leq 10^5$99 行未改动struct edge { int to, cap, rev; //to 代表要去到的点 //cap 代表流量上限(最大流中用到,其实就是选择代价) //rev 是在 to 的邻接表中起点的下标,便于回溯 //rev 是在 to 的邻接表中起点的下标,便于回溯};
int depth[maxn], iter[maxn], s, t, mxnodes, n;//depth 是在当前bfs的深度,iter为当前弧优化数组,非常重要8 行未改动}
bool bfs() {//bfs计算深度以及连通性 for(int i = 0; i <= mxnodes; i++) depth[i] = -1; depth[s] = 0; depth[s] = 0; queue<int> q; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); for(auto &e : adj[u]) { if(e.cap > 0 && depth[e.to] == -1) { depth[e.to] = depth[u] + 1; q.push(e.to); } } } return depth[t] != -1; return depth[t] != -1;}
int dfs(int v, int flow) {//dfs计算阻塞流 if(v == t) return flow;49 行未改动 cin >> n; p.resize(n); q.resize(n); for(int i = 0; i < n; i++) cin >> p[i]; for(int i = 0; i < n; i++) cin >> q[i];
int pcyc = 0, qcyc = 0; vector<int> pid = getcyc(p, pcyc); vector<int> qid = getcyc(q, qcyc);
s = 0, t = 1; int pbase = 2, qbase = 2 + pcyc; mxnodes = qbase + qcyc;
int base = 0; for(int i = 0; i < n; i++) { bool pisfixed = (p[i] == i); bool qisfixed = (q[i] == i); int u = pbase + pid[i]; int v = qbase + qid[i];
if(pisfixed && qisfixed) {//情况在上面都有解释 base++; } else if(pisfixed && !qisfixed) { addedge(s, v, 1);14 行未改动