hdu 3277 Marriage Match III(最大流,二分,并查集)
2016-08-02 16:33
381 查看
Marriage Match III
Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1994 Accepted Submission(s): 586
Problem Description
Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the ever game of play-house . What a happy time as so many friends play 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. As 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. On the other hand, in order to
play more times of marriage match, every girl can accept any K boys. If a girl chooses a boy, the boy must accept her unconditionally whether they had quarreled before or not.
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 an integer T, means the number of test cases.
Each test case starts with three integer n, m, K and f in a line (3<=n<=250, 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 1 2
1 1
2 3
3 2
4 2
4 4
1 4
2 3
Sample Output
3
Author
starvae
题意:有n个女生和n个男生,m个关系,表示某个女生和某个男生关系好,然后再给出女生之间的f个关系,表示某个女生与另一个女生关系好,一个女生可以跟k个关系不好的男生变得关系好
两个女生是朋友的话,跟某个女生关系好的男生也会跟女生的朋友关系好
现在女生每轮都选择不同的男生,问最多可以选择几轮
思路:和上一题差不多,多了一个k,那么只要把每个女生拆成两个点,另一个点往关系不好的男生那连k条边即可
注意用Dinic会超时,这里找了半天的ISAP,终于找到一个很快的模板
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 1000
#define INF 99999999
struct Edge
{
int v,next,cap;
} edge[N*N*2];
struct E
{
int u,v,cap;
} e[N*N];
int pre
;
int cnt,head
,gap
,dis
,start,End,ans;
int vis
;
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cap)
{
edge[cnt].v=v;
edge[cnt].cap=cap;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].v=u;
edge[cnt].cap=0;
edge[cnt].next=head[v];
head[v]=cnt++;
}
int finds(int x)
{
int r=x;
while(r!=pre[r])
r=pre[r];
while(x!=pre[x])
{
int l=x;
x=pre[x];
pre[l]=r;
}
return r;
}
void mix(int a,int b)
{
int x=finds(a),y=finds(b);
if(x!=y) pre[x]=y;
}
int dfs(int u,int minflow)
{
if(u==End)return minflow;
int i,v,f,min_dis=ans-1,flow=0;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(edge[i].cap<=0)continue;
if(dis[v]+1==dis[u])
{
f=dfs(v,edge[i].cap>minflow-flow?minflow-flow:edge[i].cap);
edge[i].cap-=f;
edge[i^1].cap+=f;
flow+=f;
if(flow==minflow)break;
if(dis[start]>=ans)return flow;
}
min_dis=min_dis>dis[v]?dis[v]:min_dis;
}
if(flow==0)
{
if(--gap[dis[u]]==0)
dis[start]=ans;
dis[u]=min_dis+1;
gap[dis[u]]++;
}
return flow;
}
int isap()///ans等于End+1
{
int maxflow=0;
memset(gap,0,sizeof(gap));
memset(dis,0,sizeof(dis));
gap[0]=ans;
while(dis[start]<ans)
maxflow+=dfs(start,INF);
return maxflow;
}
int main()
{
int T,n,m,k,f;
int u,v;
scanf("%d",&T);
while(T--)
{
scanf("%d %d %d %d",&n,&m,&k,&f);
for(int i=1; i<=n; i++)
pre[i]=i;
for(int i=1; i<=m; i++)
scanf("%d %d",&e[i].u,&e[i].v);
for(int i=1; i<=f; i++)
{
scanf("%d %d",&u,&v);
mix(u,v);
}
for(int i=1; i<=n; i++)
pre[i]=finds(i);
start=0,End=3*n+1,ans=End+1;
int l=0,r=n,ans;
int tt=cnt;
memset(vis,0,sizeof(vis));
for(int i=1; i<=m; i++)
{
int x=pre[e[i].u];
vis[x][e[i].v]=1;
}
while(l<=r)
{
init();
int mid=(l+r)>>1;
for(int i=1; i<=n; i++)
addedge(start,i,mid);
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(vis[pre[i]][j])
addedge(i,2*n+j,1);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
if(!vis[pre[i]][j])
addedge(n+i,2*n+j,1);
addedge(i,n+i,k);
}
for(int i=2*n+1; i<=3*n; i++)
addedge(i,End,mid);
if(isap()==mid*n) ans=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- HDU 3277 Marriage Match III【并查集+二分+最大流】
- HDU 3277 Marriage Match III(并查集+二分+最大流)
- hdoj 3277 Marriage Match III 【最大流经典建图】【二分 + 最大流 + 并查集】
- HDU 3277 Marriage Match III(二分+最大流)
- hdu 3277 Marriage Match III【并查集+最大流Dinic+拆点+建图+二分查找】
- hdu 3277 Marriage Match III(二分最大流+并查集+判断满流+拆点)
- HDU 3277 Marriage Match III(二分+最大流)
- HDU 3277 Marriage Match III(并查集+二分+最大流)
- HDU 3277 Marriage Match III(并查集+二分+最大流)
- HDU 3277 Marriage Match III(中等,好题)[二分最大流]+拆点
- hdu 3277 Marriage Match III【最大流+并查集+二分枚举】
- HDU 3277 Marriage Match III(拆点+二分+最大流SAP)
- hdu 3081 Marriage Match II(二分, 并查集,最大流)
- HDU -- 3277 Marriage Match III(最大流+二分+并查集+拆点)
- HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)
- 【hdu】3277 Marriage Match III【最大流】
- HDU 3277 Marriage Match III(最大流+二分+并查集)
- HDU 3277 Marriage Match III (最大流+拆点+并查集)
- hdu 3081 Marriage Match II(最大流 + 二分 + 并查集)
- HDU_3277_Marriage Match III(最大流)