Marriage Match II(二分+并查集+最大流,好题)
Marriage Match II
http://acm.hdu.edu.cn/showproblem.php?pid=3081
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5420 Accepted Submission(s): 1739
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?
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.
1
4 5 2
1 1
2 3
3 2
4 2
4 4
1 4
2 3
用并查集合并女生的关系,再用二分跑最大流,因为次数具有单调性,所以可以二分
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#define maxn 200005
#define MAXN 200005
#define mem(a,b) memset(a,b,sizeof(a))
const int N=;
const int M=;
const int INF=0x3f3f3f3f;
using namespace std;
int n;
struct Edge{
int v,next;
int cap,flow;
}edge[MAXN*];//注意这里要开的够大。。不然WA在这里真的想骂人。。问题是还不报RE。。
int cur[MAXN],pre[MAXN],gap[MAXN],path[MAXN],dep[MAXN];
int cnt=;//实际存储总边数
void isap_init()
{
cnt=;
memset(pre,-,sizeof(pre));
}
void isap_add(int u,int v,int w)//加边
{
edge[cnt].v=v;
edge[cnt].cap=w;
edge[cnt].flow=;
edge[cnt].next=pre[u];
pre[u]=cnt++;
}
void add(int u,int v,int w){
isap_add(u,v,w);
isap_add(v,u,);
}
bool bfs(int s,int t)//其实这个bfs可以融合到下面的迭代里,但是好像是时间要长
{
memset(dep,-,sizeof(dep));
memset(gap,,sizeof(gap));
gap[]=;
dep[t]=;
queue<int>q;
while(!q.empty())
q.pop();
q.push(t);//从汇点开始反向建层次图
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=pre[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(dep[v]==-&&edge[i^].cap>edge[i^].flow)//注意是从汇点反向bfs,但应该判断正向弧的余量
{
dep[v]=dep[u]+;
gap[dep[v]]++;
q.push(v);
//if(v==sp)//感觉这两句优化加了一般没错,但是有的题可能会错,所以还是注释出来,到时候视情况而定
//break;
}
}
}
return dep[s]!=-;
}
int isap(int s,int t)
{
if(!bfs(s,t))
return ;
memcpy(cur,pre,sizeof(pre));
//for(int i=1;i<=n;i++)
//cout<<"cur "<<cur[i]<<endl;
int u=s;
path[u]=-;
int ans=;
while(dep[s]<n)//迭代寻找增广路,n为节点数
{
if(u==t)
{
int f=INF;
for(int i=path[u];i!=-;i=path[edge[i^].v])//修改找到的增广路
f=min(f,edge[i].cap-edge[i].flow);
for(int i=path[u];i!=-;i=path[edge[i^].v])
{
edge[i].flow+=f;
edge[i^].flow-=f;
}
ans+=f;
u=s;
continue;
}
bool flag=false;
int v;
for(int i=cur[u];i!=-;i=edge[i].next)
{
v=edge[i].v;
if(dep[v]+==dep[u]&&edge[i].cap-edge[i].flow)
{
cur[u]=path[v]=i;//当前弧优化
flag=true;
break;
}
}
if(flag)
{
u=v;
continue;
}
int x=n;
if(!(--gap[dep[u]]))return ans;//gap优化
for(int i=pre[u];i!=-;i=edge[i].next)
{
if(edge[i].cap-edge[i].flow&&dep[edge[i].v]<x)
{
x=dep[edge[i].v];
cur[u]=i;//常数优化
}
}
dep[u]=x+;
gap[dep[u]]++;
if(u!=s)//当前点没有增广路则后退一个点
u=edge[path[u]^].v;
}
return ans;
} int m,d;
struct sair{
int x,y;
}p[maxn];
int Friend[][];
int fa[maxn]; int Find(int x){
int r=x,y;
while(x!=fa[x]){
x=fa[x];
}
while(r!=x){
y=fa[r];
fa[r]=x;
r=y;
}
return x;
} void join(int x,int y){
int xx=Find(x);
int yy=Find(y);
if(xx!=yy){
fa[xx]=yy;
}
}
int tmp;
int Check(int mid){
isap_init();
int s=,t=n+n+;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(Friend[i][j]){
add(i,j+n,);
}
}
}
for(int i=;i<=n;i++){
add(s,i,mid);
add(n+i,t,mid);
}
n=n+n+;
int tttt=isap(s,t);
n=tmp;
return tttt;
} int main(){
std::ios::sync_with_stdio(false);
int T;
cin>>T;
for(int co=;co<=T;co++){
cin>>n>>m>>d;
tmp=n;
memset(Friend,,sizeof(Friend));
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=m;i++) cin>>p[i].x>>p[i].y;
for(int i=m+;i<=m+d;i++) cin>>p[i].x>>p[i].y;
for(int i=m+;i<=m+d;i++) join(p[i].x,p[i].y);
for(int i=;i<=m;i++){
for(int j=;j<=n;j++){
if(Find(p[i].x)==Find(j)&&!Friend[j][p[i].y]){
Friend[j][p[i].y]=;
}
}
}
int L=,R=n,mid;
while(L<=R){
mid=(L+R)>>;
n=tmp;
if(Check(mid)>=(n*mid)){
L=mid+;
}
else{
R=mid-;
}
}
cout<<R<<endl;
}
}
Marriage Match II(二分+并查集+最大流,好题)的更多相关文章
- hdu3081 Marriage Match II(二分+并查集+最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意: n个女生与n个男生配对,每个女生只能配对某些男生,有些女生相互是朋友,每个女生也可以跟她 ...
- HDU 3081 Marriage Match II (二分+并查集+最大流)
题意:N个boy和N个girl,每个女孩可以和与自己交友集合中的男生配对子;如果两个女孩是朋友,则她们可以和对方交友集合中的男生配对子;如果女生a和女生b是朋友,b和c是朋友,则a和c也是朋友.每一轮 ...
- HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 3081 Marriage Match II 二分 + 网络流
Marriage Match II 题意:有n个男生,n个女生,现在有 f 条男生女生是朋友的关系, 现在有 m 条女生女生是朋友的关系, 朋友的朋友是朋友,现在进行 k 轮游戏,每轮游戏都要男生和女 ...
- hdu 3081(二分+并查集+最大流||二分图匹配)
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)
HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...
- HDU 3081 Marriage Match II (二分图,并查集)
HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...
- HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流
题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others) ...
- hdu3081 Marriage Match II(最大流)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Marriage Match II Time Limit: 2000/1000 M ...
随机推荐
- 解决Ubuntu下使用命令行subl 打开Sublime text3无法输入中文的问题
cd /opt/sublime_text/ sudo vim sub-fcitx.c 新建文件sub-fcitx.c,建议放在Sublime Text的所在目录下,将下面的代码复制进去 ,参考: ht ...
- 2天时间终于把ntopng装好了
1.环境centos6.7x642.安装步骤,首先把centos按优化步骤完成3.更改centos的yum源,更改为阿里云的源.4.[root@netmon ntopng]# cat /etc/yum ...
- solr亿万级索引优化实践(四)
本篇是这个系类的最后一篇,但优化方案不仅于此,需要后续的研究与学习,本篇主要从schema设计的角度来做一些实践. schema.xml 这个文件的作用是定义索引数据中的域的,包括域名称,域类型,域是 ...
- PyQt5系列教程(八)定时器QTimer的使用
软硬件环境 OS X EI Capitan Python 3.5.1 PyQt 5.5.1 前言 如果需要在程序中周期性地进行某项操作,比如检测某种设备的状态,就会用到定时器.本文就来看看PyQT5中 ...
- 红帽配置Centos仓库[红帽Redhat7替换Centos7网络源]
1.卸载红帽yum源 rpm -e $(rpm -qa|grep yum) --nodeps 2.删除所有repo相关文件 rm -rf /etc/yum.conf rm -rf /etc/yum.r ...
- hadoop分布式快速搭建
hadoop分布式快速搭建 1.配置主节点与从节点的ssh互信:[其中在主从节点主机的/etc/hosts文件中需绑定主机名ip间的映射关系; 如,192.168.1.113 node0 192.16 ...
- 0_Simple__simpleOccupancy
计算核函数调用使得占用率,并尝试使用 runtime 函数自动优化线程块尺寸,以便提高占用率. ▶ 源代码. #include <iostream> #include "cuda ...
- Redis 通用操作1
01, 设置值 => set key value 01.1, 设置值并添加有效期 => set key value ex 秒数 或者 set key value px 毫秒数 01.2, ...
- leetcode29
class Solution { public int divide(int dividend, int divisor) { if (dividend == Integer.MIN_VALUE &a ...
- 机器学习入门-数据过采样(上采样)1. SMOTE
from imblearn.over_sampling import SMOTE # 导入 overstamp = SMOTE(random_state=0) # 对训练集的数据进行上采样,测试集的 ...