hdu 3081 【二分匹配+并查集+删边||最大路+并查集+二分枚举】

Marriage Match II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2307    Accepted Submission(s): 792

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

这道题一开始我用匈牙利+删边做的,后来做网络流时看了一位大神的博客上的一段分析深有体会,又用最大流做了一遍。

链接:http://blog.csdn.net/qq564690377/article/details/7857983

这里我就只分析二分匹配了,其实很简单,就是根据并查集将该连的关系全都连起来,然后有匈牙利跑一遍,

每跑一遍就将当前节点(女孩)与其当前匹配的节点(男孩)关系删掉,再继续跑匈牙利,直到跑出的最大

匹配数小与n为止,此时跑的遍数就是最大轮数。

现上代码:

网络流:

#include<stdio.h>

#include<string.h>

#include<algorithm>

#include<iostream>

#include<queue>

#define Min(a,b) a<b?a:b

#define inf 1000000000

#define maxn 20000

#define maxh 400

using namespace std;

typedef struct  //前向星存图

{

int to,next,w;

}node;

typedef struct

{

int x,t;

}dep;

node E[maxn];

int head[maxh],headx[maxh],deep[maxh],cnt;

int set[maxh],map[maxh][maxh];

int n,m,f;

////////////////////////////////wangluoliu//////////////////////////////

void init()

{

memset(head,-1,sizeof(head));

cnt=0;

}

void add(int a,int b,int c)

{

E[cnt].to=b;

E[cnt].w=c;

E[cnt].next=head[a];

head[a]=cnt++;

E[cnt].to=a;

E[cnt].w=0;

E[cnt].next=head[b];

head[b]=cnt++;

}

int min(int x,int y)

{

return x<y?x:y;

}

int bfs(int s,int t,int n)

{

memset(deep,255,sizeof(deep));

queue<dep>Q;

dep fir,nex;

fir.x=s;

fir.t=0;

deep[s]=0;

Q.push(fir);

while(!Q.empty())

{

fir=Q.front();

Q.pop();

for(int i=head[fir.x];i+1;i=E[i].next)

{

nex.x=E[i].to;

nex.t=fir.t+1;

if(deep[nex.x]!=-1||!E[i].w)

continue;

deep[nex.x]=nex.t;

Q.push(nex);

}

}

for(int i=0;i<=n;i++)

headx[i]=head[i];

return deep[t]!=-1;

}

int dfs(int s,int t,int flow)

{

if(s==t)

return flow;

int newflow=0;

for(int i=headx[s];i+1;i=E[i].next)

{

headx[s]=i;

int to=E[i].to;

int w=E[i].w;

if(!w||deep[to]!=deep[s]+1)

continue;

int temp=dfs(to,t,min(w,flow-newflow));

newflow+=temp;

E[i].w-=temp;

E[i^1].w+=temp;

if(newflow==flow)

break;

}

if(!newflow)deep[s]=0;

return newflow;

}

int Dinic(int s,int t,int m)

{

int sum=0;

while(bfs(s,t,m))

{

sum+=dfs(s,t,inf);

}

return sum;

}

////////////////////////////////bingchaji///////////////////////////////////

int findx(int x)

{

if(set[x]!=x)

set[x]=findx(set[x]);

return set[x];

}

void fun(int x,int y)

{

x=findx(x);

y=findx(y);

if(x!=y)

set[x]=y;

}

//////////////////////////////////////jiantu////////////////////////////

void built(int mid)

{

init();//前向星初始化

int s=2*n+1,t=2*n+2;

for(int i=1;i<=n;i++)

{

add(s,i,mid);

add(i+n,t,mid);

for(int j=1;j<=n;j++)

{

if(map[i][j])

{

add(i,j+n,1);

}

}

}

}

////////////////////////////////////////////////////////////////////////

int main()

{

int T;

int a,b;

int l,r,mid;

scanf("%d",&T);

while(T--)

{

scanf("%d%d%d",&n,&m,&f);

for(int i=0;i<=n;i++)//初始化

{

set[i]=i;

for(int j=0;j<=n;j++)

map[i][j]=0;

}

for(int i=0;i<m;i++)

{

scanf("%d%d",&a,&b);

map[a][b]=1;

}

for(int i=0;i<f;i++)

{

scanf("%d%d",&a,&b);

fun(a,b);

}

for(int i=1;i<=n;i++)

{

int xi=findx(i);

for(int j=1;j<=n;j++)

{

int yi=findx(j);

if(xi==yi&&i!=j)

{

for(int k=1;k<=n;k++)

{

if(map[j][k])

{

map[i][k]=1;

}

}

}

}

}

l=0,r=n+1;

while(l!=r-1)

{

//     printf("xcwdf\n");

mid=(l+r)/2;

built(mid);

if(Dinic(2*n+1,2*n+2,2*n+2)==n*mid)

{

l=mid;

}

else

{

r=mid;

}

}

printf("%d\n",l);

}

return 0;

}

二分匹配:

#include<stdio.h>

#include<string.h>

#include<algorithm>

#include<iostream>

#define maxn 200

using namespace std;

int markd[maxn],markx[maxn],xm[maxn][maxn];

int set[maxn];

int n,m,f;

int findx(int x)

{

if(x!=set[x])

set[x]=findx(set[x]);

return set[x];

}

void fun(int x,int y)

{

x=findx(x);

y=findx(y);

if(x!=y)

{

set[y]=x;

}

}

int xyl(int x)

{

for(int i=1;i<=n;i++)

{

if(xm[x][i]&&!markd[i])

{

markd[i]=1;

if(markx[i]==-1||xyl(markx[i]))

{

markx[i]=x;

return 1;

}

}

}

return 0;

}

int main()

{

int T;

int a,b;

scanf("%d",&T);

while(T--)

{

scanf("%d%d%d",&n,&m,&f);

memset(xm,0,sizeof(xm));

for(int i=1;i<=n;i++)

set[i]=i;

for(int i=1;i<=m;i++)

{

scanf("%d%d",&a,&b);

xm[a][b]=1;

}

for(int i=1;i<=f;i++)

{

scanf("%d%d",&a,&b);

fun(a,b);

}

for(int i=1;i<=n;i++)

{

int xi=findx(i);

for(int j=1;j<=n;j++)

{

int yi=findx(j);

if(xi==yi&&i!=j)

{

for(int k=1;k<=n;k++)

{

if(xm[j][k])

xm[i][k]=1;

}

}

}

}

int cnt=0;

while(1)

{

memset(markx,255,sizeof(markx));

int sum=0;

for(int i=1;i<=n;i++)

{

memset(markd,0,sizeof(markd));

sum+=xyl(i);

}

if(sum<n)

break;

cnt++;

for(int i=1;i<=n;i++)

{

xm[markx[i]][i]=0; //删掉当前匹配边

}

}

printf("%d\n",cnt);

}

return 0;

}

时间: 2024-08-09 11:37:22

hdu 3081 【二分匹配+并查集+删边||最大路+并查集+二分枚举】的相关文章

hdu 1068 Girls and Boys 最大独立点集 二分匹配

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 思路: 求一集合满足,两两之间没有恋爱关系 思路: 最大独立点集=顶点数-最大匹配数 这里给出的关系,看似有向边,实则是无向边,那么按照二分匹配处理的话,相当于一个人看作两个人来用,最后还是顶点数目-最大二分匹配数 因为之间一个人当作两个人用,二分匹配数目减半,即 = n - match()/2 或者也可以写成 = ( 2n -  match() )/2 更容易理解 题目中n的大小没有说明,最

HDU 3081 Marriage Match II(网络流+并查集+二分答案)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 题目: 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. W

hdu 3081 Marriage Match II (二分+最大流+并查集)

hdu 3081 Marriage Match II 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 p

hdu 3081 Marriage Match II(最大流 + 二分 + 并查集)

Marriage Match II                                                                           Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Presumably, you all have known the question of stable

Marriage Match II (hdu 3081 二分图+并查集)

Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2455    Accepted Submission(s): 837 Problem Description Presumably, you all have known the question of stable marriage match. A

HDU 1281 棋盘游戏(二分匹配 与 删边)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 根据题目描述,什么是重要点?在求出最大匹配后,进行枚举,依次删边,看最大匹配数会不会发生改变,改变的话,那么该点就是重要点. #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <

HDU 2119 Matrix 简单二分匹配

行做x集,列做y集,1就给该行该列连一条边,输出最大匹配边即可 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<set> using namespace std; #define N 105 int lef[N], pn;//lef[v]表示Y集的点v 当前连接的点 , pn为x点集的

hdu 1498 50 years, 50 colors 二分匹配

50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1789    Accepted Submission(s): 978 Problem Description On Octorber 21st, HDU 50-year-celebration, 50-color balloons floating

HDU 3081 Marriage Match II(二分+最大流)

HDU 3081 Marriage Match II 题目链接 题意:n个女孩n个男孩,每个女孩可以和一些男孩配对,然后有些女孩是朋友,满足这个朋友圈里面的人,如果有一个能和某个男孩配对,其他就都可以,然后每轮要求每个女孩匹配到一个男孩,且每轮匹配到的都不同,问最多能匹配几轮 思路:二分轮数k,然后建图为,源点连向女孩,男孩连向汇点容量都为k,然后女孩和男孩之间连边为,有关系的连边容量1,这样一个匹配对应一条边,且不会重复,每次判断最大流是否等于n * k即可 代码: #include <cst