hdu 3081 二分+最大流
2014-01-18 02:49
344 查看
Marriage Match II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1435 Accepted Submission(s): 512
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each case, output a number in one line. The maximal number of Marriage Match the children can play.
[align=left]Sample Input[/align]
1
4 5 2
1 1
2 3
3 2
4 2
4 4
1 4
2 3
[align=left]Sample Output[/align]
2
题意:已知n个男孩,n个女孩,已知女孩可以匹配的男孩,已知女孩之间的朋友关系,玩配对游戏,每一个女孩每一次都必须与不同的男孩配对,问最多可以玩几轮游戏。
解题思路:将女孩与可以匹配的男孩之间建流量为1的边,二分轮数,源点与女孩连边,男孩与汇点连边,然后跑最大流。
代码:
/* ***********************************************
Author :xianxingwuguan
Created Time :2014/1/18 0:58:25
File Name :E.cpp
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn=10000;
int head[1000],tol,dep[1000],n;
struct node
{
int next,to,from,cap;
}edge[30000];
void add(int u,int v,int cap)
{
edge[tol].from=u;
edge[tol].to=v;
edge[tol].cap=cap;
edge[tol].next=head[u];
head[u]=tol++;
edge[tol].from=v;
edge[tol].to=u;
edge[tol].cap=0;
edge[tol].next=head[v];
head[v]=tol++;
}
int bfs(int s,int t)
{
int que[maxn],front=0,rear=0;
memset(dep,-1,sizeof(dep));
dep[s]=0;que[rear++]=s;
while(front!=rear)
{
int u=que[front++];front%=maxn;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap>0&&dep[v]==-1)
{
dep[v]=dep[u]+1;
que[rear++]=v;
rear%=maxn;
if(v==t)return 1;
}
}
}
return 0;
}
int dinic(int s,int t)
{
int i,res=0,top;
int Stack[maxn],cur[maxn];
while(bfs(s,t))
{
memcpy(cur,head,sizeof(head));
int u=s;top=0;
while(1)
{
if(u==t)
{
int min=inf,loc;
for(int i=0;i<top;i++)
if(min>edge[Stack[i]].cap)
{
min=edge[Stack[i]].cap;
loc=i;
}
for(int i=0;i<top;i++)
{
edge[Stack[i]].cap-=min;
edge[Stack[i]^1].cap+=min;
}
res+=min;
top=loc;
u=edge[Stack[top]].from;
}
for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)
if(edge[i].cap&&dep[u]+1==dep[edge[i].to])break;
if(cur[u]!=-1)
{
Stack[top++]=cur[u];
u=edge[cur[u]].to;
}
else
{
if(top==0)break;
dep[u]=-1;
u=edge[Stack[--top]].from;
}
}
}
return res;
}
int fa[1000];
int find(int x)
{
if(fa[x]!=x)
fa[x]=find(fa[x]);
return fa[x];
}
int vis[300][333];
bool judge(int mid)
{
memset(head,-1,sizeof(head));tol=0;
for(int i=1;i<=n;i++)
{
add(0,i,mid);
add(i+n,2*n+1,mid);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(vis[i][j])
add(i,j+n,1);
}
if(dinic(0,2*n+1)==mid*n)
return 1;
return 0;
}
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int T,i,j,k,m,f;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&f);
memset(head,-1,sizeof(head));tol=0;
memset(vis,0,sizeof(vis));
vector<int> ss[500];
while(m--)
{
scanf("%d%d",&i,&j);
vis[i][j]=1;
ss[i].push_back(j);
}
for(i=1;i<=n;i++)fa[i]=i;
while(f--)
{
scanf("%d%d",&i,&j);
int fx=find(i);
int fy=find(j);
if(fx==fy)continue;
fa[fx]=fy;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(find(i)!=find(j))continue;
int cnt=ss[i].size();
for(k=0;k<cnt;k++)
vis[j][ss[i][k]]=1;
cnt=ss[j].size();
for(k=0;k<cnt;k++)
vis[i][ss[j][k]]=1;
}
}
int left=0,right=n,mid;
while(left<right)
{
mid=(left+right+1)>>1;
if(judge(mid))left=mid;
else right=mid-1;
}
printf("%d\n",left);
}
return 0;
}
相关文章推荐
- 最长重复子串和最长不重复子串求解
- oracle study road
- oracle study road
- 求S字符串中最长重复子串
- [unity3d]导出安卓版设置
- [unity3d]导出安卓版设置
- 第七节:Struts2-类型转换
- CSS样式优先级
- Spark On YARN 环境搭建
- ubuntu12 安装Pig version 0.12.0
- LeetCode - Swap Nodes in Pairs
- LeetCode(108)Convert Sorted Array to Binary Search Tree
- ios 字符串处理
- shell实现数学运算函数
- 网站投票是什么
- 搜索引擎网页快照工作原理
- 实现Com连接点产生异常(0x80020010 无效的被呼叫方)解决办法
- Websocket On Bash
- Codeforces 382 C. Arithmetic Progression
- W3A SCAN 开发过程记录