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
 
 
 
【题意】

  n个女生与n个男生配对,每个女生只能配对某些男生,有些女生相互是朋友,每个女生也可以跟她朋友能配对的男生配对。

每次配对,每个女生都要跟不同的男生配对且每个女生都能配到对。问最多能配对几轮。(n<=100)

【分析】

  二分答案,因为有单调性。然后直接最大流了。输入那里要用并查集。

  注意不能直接跑最大流哦,(因为你不知道跑完最大流之后ans是啥啊)

  哇塞~TLE了一辈子,dinic中几个不起眼的优化也能是可以挽救你于水火之中啊。比如下面的加粗部分:

int find_flow(int x,int flow)
{
  if(x==ed) return flow;
  int now=0;
  for(int i=first[x];i;i=t[i].next) if(t[i].f>0)
  {
    int y=t[i].y;
    if(dis[y]==dis[x]+1)
    {
      int a=find_flow(y,mymin(t[i].f,flow-now));
      t[i].f-=a;
      t[t[i].o].f+=a;
      now+=a;
      if(now==flow) break;
    }
  }
  if(now==0) dis[x]=-1;
  return now;
}

  我这个傻逼还因为数组开小了TLE了好一阵子。

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 210
#define Maxm 10010
#define INF 0xfffffff int fa[Maxn],first[Maxn],dis[Maxn];
bool map[Maxn][Maxn]; struct node
{
int x,y,f,o,next;
};
node t[*Maxm],tt[*Maxm];int len; int st,ed;
int n,m,k; int mymin(int x,int y) {return x<y?x:y;} void ins(int x,int y,int f)
{
tt[++len].x=x;tt[len].y=y;tt[len].f=f;
tt[len].next=first[x];first[x]=len;tt[len].o=len+;
tt[++len].x=y;tt[len].y=x;tt[len].f=;
tt[len].next=first[y];first[y]=len;tt[len].o=len-;
} int ffind(int x)
{
if(fa[x]!=x) fa[x]=ffind(fa[x]);
return fa[x];
} queue<int > q;
bool bfs()
{
while(!q.empty()) q.pop();
memset(dis,-,sizeof(dis));
q.push(st);dis[st]=;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=first[x];i;i=t[i].next) if(t[i].f>)
{
int y=t[i].y;
if(dis[y]==-)
{
dis[y]=dis[x]+;
q.push(y);
}
}
}
if(dis[ed]==-) return ;
return ;
} int find_flow(int x,int flow)
{
if(x==ed) return flow;
int now=;
for(int i=first[x];i;i=t[i].next) if(t[i].f>)
{
int y=t[i].y;
if(dis[y]==dis[x]+)
{
int a=find_flow(y,mymin(t[i].f,flow-now));
t[i].f-=a;
t[t[i].o].f+=a;
now+=a;
if(now==flow) break;
}
}
if(now==) dis[x]=-;
return now;
} int max_flow()
{
int ans=;
while(bfs())
ans+=find_flow(st,INF);
return ans;
} bool check(int x)
{
for(int i=;i<=len;i++) t[i]=tt[i];
for(int i=len-*n+;i<=len;i+=) t[i].f=x;
return max_flow()==n*x;
} void finda()
{
int l=,r=n*n;
while(l<r)
{
int mid=(l+r+)>>;
if(check(mid)) l=mid;
else r=mid-;
}
printf("%d\n",l);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
len=;
memset(first,,sizeof(first));
memset(map,,sizeof(map));
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
map[x][y]=;
}
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=k;i++)
{
int x,y;
scanf("%d%d",&x,&y);
fa[ffind(x)]=ffind(y);
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++) if(ffind(i)==ffind(j))
{
for(int k=;k<=n;k++) if(map[j][k])
map[i][k]=;
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++) if(map[i][j])
ins(i,j+n,);
st=*n+;ed=st+;
for(int i=;i<=n;i++) ins(st,i,INF);
for(int i=;i<=n;i++) ins(i+n,ed,INF);
finda();
}
return ;
}

[HDU3081]

  所以现在我的数组是能开多大开多大。

2016-05-29 15:09:33

【HDU3081】Marriage Match II (二分+最大流)的更多相关文章

  1. hdu3081 Marriage Match II(二分+并查集+最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意: n个女生与n个男生配对,每个女生只能配对某些男生,有些女生相互是朋友,每个女生也可以跟她 ...

  2. HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流

    题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    ...

  3. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

  4. HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))

    Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. Marriage Match II(二分+并查集+最大流,好题)

    Marriage Match II http://acm.hdu.edu.cn/showproblem.php?pid=3081 Time Limit: 2000/1000 MS (Java/Othe ...

  6. hdu3081 Marriage Match II(最大流)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Marriage Match II Time Limit: 2000/1000 M ...

  7. HDU 3081 Marriage Match II 二分 + 网络流

    Marriage Match II 题意:有n个男生,n个女生,现在有 f 条男生女生是朋友的关系, 现在有 m 条女生女生是朋友的关系, 朋友的朋友是朋友,现在进行 k 轮游戏,每轮游戏都要男生和女 ...

  8. HDU 3081 Marriage Match II (二分+并查集+最大流)

    题意:N个boy和N个girl,每个女孩可以和与自己交友集合中的男生配对子;如果两个女孩是朋友,则她们可以和对方交友集合中的男生配对子;如果女生a和女生b是朋友,b和c是朋友,则a和c也是朋友.每一轮 ...

  9. hdu3081 Marriage Match II

    新年第一篇,又花了一早上,真是蠢啊! 二分+网络流 之前对于讨论哪些人是朋友的时候复杂度过高 直接n3的暴力虽然看起来复杂度高,其实并不是每次都成立 #include<bits/stdc++.h ...

  10. HDU 3081 Marriage Match II(二分法+最大流量)

    HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个 ...

随机推荐

  1. Eclipse 浏览文件插件 EasyExplorer 和 OpenExplorer

    EasyExplorer  是一个类似于 Windows Explorer的Eclipse插件,它可以帮助你在不退出Eclipse的环境下浏览本地文件系统 下载地址: 从 http://sourcef ...

  2. 使用Windows的NAT功能

    使用管理员权限打开命令行控制台. 端口映射相关命令 查看当前机器的端口代理表: netsh interface portproxy show all C:\WINDOWS\system32>ne ...

  3. Modem常用概念

    真实设备的标识,即DEVICE_ID.比如,Android设备是手机,这个DEVICE_ID可以同通过TelephonyManager.getDeviceId()获取,它根据不同的手机设备返回IMEI ...

  4. web前端开发中的浏览器兼容性总结

    1.居中问题 div里的内容,IE默认为居中,而FF默认为左对齐,可以尝试增加代码margin: 0 auto; 2.高度问题 两上下排列或嵌套的div,上面的div设置高度(height),如果di ...

  5. [GDI+] C# ImageDown帮助类教程与源码下载 (转载)

    点击下载 ImageDown.zip 1.下载图片到本地代码如下 /// <summary> /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url=h ...

  6. Udacity(优达学城)300块红包优惠券

    纳米学位:来自硅谷的名企官方课程 7天免费试用结束后,在"我的教室->设置->纳米学位->续费"页面上的优惠码区域,输入AF55BA53,立即减300元:

  7. Android基础问题汇总

    一.android:gravity 和android:layout_gravity的区别: android;gravity是自己的内容相对于自己的控件的位置,而android:layout_gravi ...

  8. AbstractMethodError using UriBuilder on JAX-RS

    问题描述:Eclipse调试JAX-RS服务没问题,但是在发布服务端时候抛出异常 java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder. ...

  9. 跟着老男孩一步步学习Shell高级编程实战

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://oldboy.blog.51cto.com/2561410/1264627 本sh ...

  10. spring配置文件中属性mappingLocations、mappingDirectoryLocations

    http://blog.csdn.net/vacblog/article/details/7774173