题意:有n个男生,n个女生,现在有 f 条男生女生是朋友的关系, 现在有 m 条女生女生是朋友的关系, 朋友的朋友是朋友,现在进行 k 轮游戏,每轮游戏都要男生和女生配对,每轮配对过的人在接下来中都不能配对,求这个k最大是多少。
题解:二分 + 网络流check 。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb emplace_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = (int)1e9+;
const int N = ;
const int M = N * N;
int head[N], deep[N], cur[N];
int w[M], to[M], nx[M];
int tot;
void add(int u, int v, int val){
w[tot] = val; to[tot] = v;
nx[tot] = head[u]; head[u] = tot++; w[tot] = ; to[tot] = u;
nx[tot] = head[v]; head[v] = tot++;
}
int bfs(int s, int t){
queue<int> q;
memset(deep, , sizeof(deep));
q.push(s);
deep[s] = ;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = nx[i]){
if(w[i] > && deep[to[i]] == ){
deep[to[i]] = deep[u] + ;
q.push(to[i]);
}
}
}
return deep[t] > ;
}
int Dfs(int u, int t, int flow){
if(u == t) return flow;
for(int &i = cur[u]; ~i; i = nx[i]){
if(deep[u]+ == deep[to[i]] && w[i] > ){
int di = Dfs(to[i], t, min(w[i], flow));
if(di > ){
w[i] -= di, w[i^] += di;
return di;
}
}
}
return ;
} int Dinic(int s, int t){
int ans = , tmp;
while(bfs(s, t)){
for(int i = ; i <= t; i++) cur[i] = head[i];
while(tmp = Dfs(s, t, inf)) ans += tmp;
}
return ans;
} void init(){
memset(head, -, sizeof(head));
tot = ;
}
int pre[N];
int link[N][N];
int edge[M][];
int Find(int x){
if(x == pre[x]) return x;
return pre[x] = Find(pre[x]);
}
int T, n, m, f, s, t, u, v;
void GG(int val){
for(int i = head[s]; ~i; i = nx[i]){
if(i&);
else w[i] = val, w[i+] = ;
}
for(int i = n+; i <= n+n; i++){
for(int j = head[i]; ~j; j = nx[j]){
if(j&);
else w[j] = val, w[j+] = ;
}
}
for(int i = ; i <= n; i++){
for(int j = head[i]; ~j; j = nx[j]){
if(j&);
else w[j] = , w[j+] = ;
}
}
}
int main(){
scanf("%d", &T);
while(T--){
init();
scanf("%d%d%d", &n, &m, &f);
for(int i = ; i <= n; i++){
pre[i] = i;
for(int j = ; j <= n; j++)
link[i][j] = ;
}
for(int i = ; i <= m; i++)
scanf("%d%d", &edge[i][], &edge[i][]);
for(int i = ; i <= f; i++){
scanf("%d%d", &u, &v);
u = Find(u);
v = Find(v);
if(v == u) continue;
pre[u] = v;
}
for(int i = ; i <= m; i++){
int z = Find(edge[i][]);
link[z][edge[i][]] = ;
}
s = , t = * n + ;
for(int i = ; i <= n; i++){
add(s, i, );
int z = Find(i);
for(int j = ; j <= n; j++){
if(link[z][j])
add(i, j+n, );
}
add(i+n,t,);
}
int l = , r = n;
while(l <= r){
int mid = l+r >> ;
GG(mid);
if(Dinic(s,t) == mid*n) l = mid + ;
else r = mid - ;
}
printf("%d\n", l-);
}
return ;
}