首页 > 其他分享 >CodeForces 1783F Double Sort II

CodeForces 1783F Double Sort II

时间:2023-01-19 17:44:20浏览次数:69  
标签:Sort int Double flow len II edges maxn long

洛谷传送门

CF 传送门

考虑只有一个排列怎么做。有一个结论是答案为 \(n\ -\) 置换环个数,即每个环都会选择一个点不操作,其他点都操作。

接下来考虑两个排列,显然当 \(x\) 在 \(a\) 和 \(b\) 中都不操作,\(x\) 才能不操作。设 \(x\) 在 \(a\) 中所在环为 \(pa_x\),在 \(b\) 中所在环为 \(pb_x\),那么若 \(x\) 操作了,那么 \(pa_x\) 和 \(pb_x\) 的其他所有点都要操作。可以看成是二分图匹配,每条匹配边表示 \(x\) 不被操作,于是答案为 \(n\ -\) 最大匹配。

至于求方案,若 \(x\) 连的那条匹配边未满流,那么 \(x\) 就要被操作。

code
/*

p_b_p_b txdy
AThousandSuns txdy
Wu_Ren txdy
Appleblue17 txdy

*/

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 300100;
const int inf = 0x3f3f3f3f;

int n, a[maxn], b[maxn], head[maxn], len = 1, S, T, ntot;

struct edge {
	int to, next, cap, flow;
} edges[maxn];

void add_edge(int u, int v, int c, int f) {
	edges[++len].to = v;
	edges[len].next = head[u];
	edges[len].cap = c;
	edges[len].flow = f;
	head[u] = len;
}

struct dsu {
	int fa[maxn];
	
	void init() {
		for (int i = 1; i <= n; ++i) {
			fa[i] = i;
		}
	}
	
	int find(int x) {
		return fa[x] == x ? x : fa[x] = find(fa[x]);
	}
	
	void merge(int x, int y) {
		x = find(x);
		y = find(y);
		if (x != y) {
			fa[x] = y;
		}
	}
} d1, d2;

struct Dinic {
	int d[maxn], cur[maxn];
	bool vis[maxn];
	
	void add(int u, int v, int c) {
		add_edge(u, v, c, 0);
		add_edge(v, u, 0, 0);
	}
	
	bool bfs() {
		queue<int> q;
		for (int i = 1; i <= ntot; ++i) {
			d[i] = -1;
			vis[i] = 0;
		}
		q.push(S);
		d[S] = 0;
		vis[S] = 1;
		while (q.size()) {
			int u = q.front();
			q.pop();
			for (int i = head[u]; i; i = edges[i].next) {
				edge &e = edges[i];
				if (!vis[e.to] && e.cap > e.flow) {
					vis[e.to] = 1;
					d[e.to] = d[u] + 1;
					q.push(e.to);
				}
			}
		}
		return vis[T];
	}
	
	int dfs(int u, int a) {
		if (!a || u == T) {
			return a;
		}
		int flow = 0, f;
		for (int &i = cur[u]; i; i = edges[i].next) {
			edge &e = edges[i];
			if (d[e.to] == d[u] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) {
				e.flow += f;
				edges[i ^ 1].flow -= f;
				flow += f;
				a -= f;
				if (!a) {
					break;
				}
			}
		}
		return flow;
	}
	
	int solve() {
		int flow = 0;
		while (bfs()) {
			for (int i = 1; i <= ntot; ++i) {
				cur[i] = head[i];
			}
			flow += dfs(S, inf);
		}
		return flow;
	}
} solver;

void solve() {
	scanf("%d", &n);
	d1.init();
	d2.init();
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
		d1.merge(i, a[i]);
	}
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &b[i]);
		d2.merge(i, b[i]);
	}
	S = n * 2 + 1;
	T = ntot = n * 2 + 2;
	for (int i = 1; i <= n; ++i) {
		solver.add(d1.find(i), d2.find(i) + n, 1);
	}
	for (int i = 1; i <= n; ++i) {
		if (d1.fa[i] == i) {
			solver.add(S, i, 1);
		}
		if (d2.fa[i] == i) {
			solver.add(i + n, T, 1);
		}
	}
	int flow = solver.solve();
	printf("%d\n", n - flow);
	for (int i = 1; i <= n; ++i) {
		if (!edges[i * 2].flow) {
			printf("%d ", i);
		}
	}
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:Sort,int,Double,flow,len,II,edges,maxn,long
From: https://www.cnblogs.com/zltzlt-blog/p/17061892.html

相关文章