题目链接:Marriage Match II
题目大意:有2n个人,n个女孩,n个男孩,女孩有自己心仪的对象,在女孩中有f对朋友,朋友之间心仪的对象是可以传递的,求最多可以匹配多少次。
解题思路: 这道题要用到并查集网络流和二分搜索。首先不要急于先建边,现将关系储存下来,接着用并查集处理朋友关系,然后二分搜索答案。在二分搜索的时候建图,将源点和女孩相连,权值为二分的答案(mid),将男孩和汇点相连权值为mid,接着枚举女孩将她和她的朋友与她心仪的对象相连。二分的条件为跑一遍Dinic的结果和n * mid是否相等。
代码如下:
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 0x3f3f3f3f;
const int maxn = 500 + 15;
const int maxm = 1e6 + 15;
struct Edge {
int to, cap, next;
};
int ecnt, st, ed, n, m, f;
Edge es[maxm];
int x[maxm], y[maxm];
int cur[500], dep[500], head[500];
int par[500], vis[205][205];
class Dinic {
public:
void init(){
memset(head, -1, sizeof head);
ecnt = 0;
}
void add_edge(int u, int v, int cap) {
es[ecnt].to = v;
es[ecnt].next = head[u];
es[ecnt].cap = cap;
head[u] = ecnt++;
}
void add_double(int u, int v, int w1, int w2 = 0) {
add_edge(u, v, w1);
add_edge(v, u, w2);
}
bool BFS() {
queue<int> q; q.push(st);
memset(dep, 0x3f, sizeof dep); dep[st] = 0;
while(q.size() && dep[ed] == inf) {
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = es[i].next) {
Edge& e = es[i];
if(e.cap > 0 && dep[e.to] == inf) {
dep[e.to] = dep[u] + 1;
q.push(e.to);
}
}
}
return dep[ed] < inf;
}
int DFS(int u, int maxflow) {
if(u == ed) return maxflow;
int res = 0;
for(int i = cur[u]; ~i; i = es[i].next) {
Edge& e = es[i];
if(dep[e.to] == dep[u] + 1 && e.cap > 0) {
int flow = DFS(e.to, min(maxflow, e.cap));
cur[u] = i;
res += flow; maxflow -= flow;
es[i].cap -= flow;
es[i ^ 1].cap += flow;
if(!maxflow) return res;
}
}
dep[u] = inf;
return res;
}
int MaxFlow(){
int ans = 0;
while(BFS()) {
for(int i = st; i <= ed; i++) cur[i] = head[i];
ans += DFS(st, inf);
}
return ans;
}
};
inline int read() {
char c = getchar();
while(!isdigit(c)) c = getchar();
int x = 0;
while(isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
int Find(int x) {
if(par[x] == x) return x;
return par[x] = Find(par[x]);
}
void unite(int x, int y) {
x = Find(x); y = Find(y);
if(x == y) return;
par[x] = y;
}
int main(){
#ifdef NEKO
freopen("Nya.txt", "r", stdin);
#endif
int t; t = read(); Dinic dic;
while(t--) {
n = read(); m = read(); f = read();
for(int i = 1; i <= m; i++) {
x[i] = read();
y[i] = read();
}
st = 0, ed = n * 2 + 1;
for(int i = 0; i <= n; i++) par[i] = i;
for(int i = 1; i <= f; i++) {
int u, v; u = read();
v = read();
unite(u, v);
}
int l = 0, r = n, mid, ans;
while(l <= r) {
mid = (l + r) >> 1;
dic.init(); memset(vis, 0, sizeof vis);
for(int i = 1; i <= n; i++) {
dic.add_double(st, i, mid);
dic.add_double(i + n, ed, mid);
}
for(int i = 1; i <= m; i++) {
int u = x[i], v = y[i];
for(int j = 1; j <= n; j++) {
if(Find(u) != Find(j)) continue;
if(vis[j][v]) continue;
vis[j][v] = 1;
dic.add_double(j, v + n, 1);
}
}
int res = dic.MaxFlow();
if(res == n * mid){ l = mid + 1; ans = mid; }
else r = mid - 1;
}
printf("%d\n", ans);
}
return 0;
}