HDU3081(KB11-N 二分答案+最大流)

Marriage Match II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4248    Accepted Submission(s): 1406

Problem Description

Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the game of playing house we used to play when we are kids. What a happy time as so many friends playing together. And it is normal that a fight or a quarrel breaks out, but we will still play together after that, because we are kids. 
Now, there are 2n kids, n boys numbered from 1 to n, and n girls numbered from 1 to n. you know, ladies first. So, every girl can choose a boy first, with whom she has not quarreled, to make up a family. Besides, the girl X can also choose boy Z to be her boyfriend when her friend, girl Y has not quarreled with him. Furthermore, the friendship is mutual, which means a and c are friends provided that a and b are friends and b and c are friend. 
Once every girl finds their boyfriends they will start a new round of this game—marriage match. At the end of each round, every girl will start to find a new boyfriend, who she has not chosen before. So the game goes on and on.
Now, here is the question for you, how many rounds can these 2n kids totally play this game?
 

Input

There are several test cases. First is a integer T, means the number of test cases. 
Each test case starts with three integer n, m and f in a line (3<=n<=100,0<m<n*n,0<=f<n). n means there are 2*n children, n girls(number from 1 to n) and n boys(number from 1 to n).
Then m lines follow. Each line contains two numbers a and b, means girl a and boy b had never quarreled with each other. 
Then f lines follow. Each line contains two numbers c and d, means girl c and girl d are good friends.
 

Output

For each case, output a number in one line. The maximal number of Marriage Match the children can play.
 

Sample Input

1
4 5 2
1 1
2 3
3 2
4 2
4 4
1 4
2 3
 

Sample Output

2
 

Author

starvae
 

Source

 
起初做法:
S-girl和boy-T连边,容量n;
girl和所有能匹配的boy连边,容量1;
然后跑一次最大流,答案为S-girl和boy-T的边中流量的最小值。
 
但是,发现反例:
1
3 4 0
1 2
2 1
2 3
3 2
答案应该是0,但用上述方法答案为1。
 
所以需要二分答案,即二分S-girl和boy-T连边的容量mid,建图跑最大流,若mid*n == maxflow,表示可以进行mid轮游戏,继续二分。
 
 //2017-08-25
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#define mid ((l+r)>>1) using namespace std; const int N = ;
const int M = ;
const int INF = 0x3f3f3f3f;
int head[N], tot;
struct Edge{
int next, to, w;
}edge[M]; void add_edge(int u, int v, int w){
edge[tot].w = w;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++; edge[tot].w = ;
edge[tot].to = u;
edge[tot].next = head[v];
head[v] = tot++;
} struct Dinic{
int level[N], S, T;
void init(int _S, int _T){
S = _S;
T = _T;
tot = ;
memset(head, -, sizeof(head));
}
bool bfs(){
queue<int> que;
memset(level, -, sizeof(level));
level[S] = ;
que.push(S);
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
int w = edge[i].w;
if(level[v] == - && w > ){
level[v] = level[u]+;
que.push(v);
}
}
}
return level[T] != -;
}
int dfs(int u, int flow){
if(u == T)return flow;
int ans = , fw;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to, w = edge[i].w;
if(!w || level[v] != level[u]+)
continue;
fw = dfs(v, min(flow-ans, w));
ans += fw;
edge[i].w -= fw;
edge[i^].w += fw;
if(ans == flow)return ans;
}
if(ans == )level[u] = ;
return ans;
}
int maxflow(){
int flow = ;
while(bfs())
flow += dfs(S, INF);
return flow;
}
}dinic; bool G[N][N];
int T, n, m, f;
int never_quarreled[M][], friends[M][];
void build_graph(int cap){
int s = , t = *n+;
dinic.init(s, t);
memset(G, , sizeof(G));
int a, b;
for(int i = ; i < m; i++){
a = never_quarreled[i][];
b = never_quarreled[i][];
add_edge(a, n+b, );
G[a][b] = ;
}
for(int i = ; i < f; i++){
a = friends[i][];
b = friends[i][];
for(int i = head[a]; i != -; i = edge[i].next){
int v = edge[i].to;
if(!G[b][v-n] && v != s && v != t){
add_edge(b, v, );
G[b][v-n] = ;
}
}
for(int i = head[b]; i != -; i = edge[i].next){
int v = edge[i].to;
if(!G[a][v-n] && v != s && v != t){
add_edge(a, v, );
G[a][v-n] = ;
}
}
}
for(int i = ; i <= n; i++){
add_edge(s, i, cap);
add_edge(n+i, t, cap);
}
} int main()
{
std::ios::sync_with_stdio(false);
//freopen("inputN.txt", "r", stdin);
cin>>T;
while(T--){
cin>>n>>m>>f;
for(int i = ; i < m; i++)
cin>>never_quarreled[i][]>>never_quarreled[i][];
for(int i = ; i < f; i++)
cin>>friends[i][]>>friends[i][];
int l = , r = n, ans = ;
while(l <= r){
build_graph(mid);
int flow = dinic.maxflow();
if(flow == mid*n){
ans = mid;
l = mid+;
}else{
r = mid-;
}
}
cout<<ans<<endl;
}
return ;
}
上一篇:BZOJ2547 CTSC2002玩具兵(最短路径+二分答案+最大流)


下一篇:npm 淘宝镜像安装