hdu3081 Marriage Match II(最大流)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Marriage Match II

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

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

题意:

有n个女孩和n个男孩,有m个女孩与男孩的关系,代表女孩喜欢男孩,有f个女孩与女孩的关系,代表女孩与女孩是好朋友。

若女孩i与女孩j是好朋友,而女孩i喜欢男孩k,则女孩j也可以和男孩匹配。即女孩之间的关系是可以传递的,而男孩之间的不可以。

每对男孩与女孩之间只能匹配一次,问可以进行几轮配对。

分析:
先floyd搞好女孩之间的传递关系,然后对于可以配对的女孩与男孩之间连一条容量为1的边,然后二分答案,每次二分之后从源点向各个女孩连容量为二分值的边,从各个男孩向汇点连容量为二分值的边。

 //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
#define MAXN 1010
struct edge{
int to,cap,rev;
edge(int _to,int _cap,int _rev)
{
to=_to;
cap=_cap;
rev=_rev;
}
};
const int MAX_V=;
vector<edge>G[MAX_V];
int iter[MAX_V];
int head[MAXN];
int _to[*];
int _flow[*];
int _next[*];
int level[MAX_V];
int tot=;
void add_edge(int from,int to,int cap)
{
G[from].PB(edge(to,cap,G[to].size()));
G[to].PB(edge(from,,G[from].size()-));
}
void Add(int u,int v,int f){
_to[tot]=v;
_flow[tot]=f;
_next[tot]=head[u];
head[u]=tot++;
}
void bfs(int s,int t)
{
CLR(level,-);
queue<int>q;
level[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=;i<G[u].size();i++)
{
edge &e=G[u][i];
if(e.cap>&&level[e.to]<)
{
level[e.to]=level[u]+;
q.push(e.to);
}
}
}
}
int dfs(int v,int t,int f)
{
if(v==t)return f;
for(int &i=iter[v];i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>&&level[v]<level[e.to])
{
int d=dfs(e.to,t,min(f,e.cap));
if(d>)
{
e.cap-=d;;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return ;
}
int Dinic(int s,int t)
{
int flow=;
for(;;)
{
bfs(s,t);
if(level[t]<)return flow;
memset(iter,,sizeof(iter));
int f;
while((f=dfs(s,t,INF))>)
{
flow+=f;
}
}
} int a[][]; int main()
{
ios::sync_with_stdio(false);
int t;
scanf("%d",&t);
while(t--){
int n,m,f;
scanf("%d%d%d",&n,&m,&f);
tot=;
for(int i=;i<MAXN;i++)head[i]=-;
int u,v;
memset(a,,sizeof(a));
for(int i=;i<m;i++){
scanf("%d%d",&u,&v);
a[u][v+n]=;
}
for(int i=;i<f;i++){
scanf("%d%d",&u,&v);
a[u][v]=;
a[v][u]=;
}
for(int i=;i<=n+n;i++)a[i][i]=;
for(int k=;k<=n+n;k++){
for(int i=;i<=n+n;i++){
for(int j=;j<=n+n;j++){
a[i][j]=max(a[i][j],a[i][k]&a[k][j]);
}
}
}
for(int i=;i<=n;i++){
for(int j=+n;j<=n+n;j++){
if(a[i][j])Add(i,j,);
}
}
int l=,r=n;
int s=,t=*n+;
int ans=;
while(l<=r){
int mid=(l+r)>>;
for(int i=;i<=*n+;i++)G[i].clear();
for(int i=;i<=*n;i++){
int now=head[i];
while(now!=-){
add_edge(i,_to[now],_flow[now]);
now=_next[now];
}
}
for(int i=;i<=n;i++){
add_edge(s,i,mid);
add_edge(n+i,t,mid);
}
if(Dinic(s,t)==mid*n){
ans=mid;
l=mid+;
}
else r=mid-;
}
printf("%d\n",ans);
} return ;
}

代码君

上一篇:快速提取PROTEL99SE PCB文件上的封装方法


下一篇:python趣味——与MS系列编译器一样强大的Unicode变量名支持