N - Marriage Match II - HDU 3081(最大流)
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
using namespace std; const int MAXN = ;
const int oo = 1e9+; int G[MAXN][MAXN], Layer[MAXN], N, M;
int girl[MAXN*MAXN], boy[MAXN*MAXN], father[MAXN];
vector<int>love[MAXN]; int Find(int x)
{
if(father[x] != x)
father[x] = Find(father[x]);
return father[x];
}
void InIt()
{
for(int i=; i<=N; i++)
{
father[i] = i;
love[i].clear();
}
}
void BuidGraph(int flow, int start, int End)
{
memset(G, , sizeof(G)); for(int i=; i<=N; i++)
{///源点和女孩相连,汇点和男孩相连,流量是flow
G[start][i] = flow;
G[i+N][End] = flow; int u = Find(i);///注意别用father[i]
int len = love[u].size(); for(int j=; j<len; j++)
{///女孩和男孩之间的流量是1
G[i][love[u][j]] = ;
}
}
}
bool BFS(int start, int End)
{
memset(Layer, , sizeof(Layer));
queue<int> Q;
Q.push(start);
Layer[start] = ; while(Q.size())
{
int u = Q.front();Q.pop(); if(u == End)return true; for(int v=; v<=End; v++)
{
if(Layer[v]==false && G[u][v])
{
Layer[v] = Layer[u] + ;
Q.push(v);
}
}
} return false;
}
int DFS(int u, int MaxFlow, int End)
{
if(u == End)return MaxFlow; int uflow = ; for(int v=; v<=End; v++)
{
if(Layer[v]==Layer[u]+ && G[u][v])
{
int flow = min(MaxFlow-uflow, G[u][v]);
flow = DFS(v, flow, End); G[u][v] -= flow;
G[v][u] += flow;
uflow += flow; if(uflow == MaxFlow)
break;
}
} if(uflow == )
Layer[u] = ;
return uflow;
}
int Dinic(int start, int End)
{
int MaxFlow = ; while(BFS(start, End) == true)
MaxFlow += DFS(start, oo, End); return MaxFlow;
} int main()
{
int T; scanf("%d", &T); while(T--)
{
int i, F, u, v; scanf("%d%d%d", &N, &M, &F); InIt(); for(i=; i<=M; i++)
scanf("%d%d", &girl[i], &boy[i]);
for(i=; i<=F; i++)
{///用并查集合并朋友关系
scanf("%d%d", &u, &v);
u = Find(u);
v = Find(v); if(u != v)
father[u] = v;
} for(i=; i<=M; i++)
{///把相同的朋友的男朋友全部都连接到根节点上,男生的区间N~2*N
u = Find(girl[i]);
love[u].push_back(boy[i]+N);
} int start=N*+, End = start+;
int left = , right = N, ans=; while(left <= right)
{
int Mid = (left+right)>>; BuidGraph(Mid, start, End);
int MaxFlow = Dinic(start, End); if(MaxFlow == Mid*N)
{
left = Mid + ;
ans = Mid;
}
else
right = Mid - ;
} printf("%d\n", ans);
} return ;
}
N - Marriage Match II - HDU 3081(最大流)的更多相关文章
- Marriage Match II HDU - 3081(二分权值建边)
题意: 有编号为1~n的女生和1~n的男生配对 首先输入m组,a,b表示编号为a的女生没有和编号为b的男生吵过架 然后输入f组,c,d表示编号为c的女生和编号为d的女生是朋友 进行配对的要求满足其一即 ...
- 【HDU3081】Marriage Match II (二分+最大流)
Description Presumably, you all have known the question of stable marriage match. A girl will choose ...
- hdu 3081 hdu 3277 hdu 3416 Marriage Match II III IV //最大流的灵活运用
3081 题意: n个女孩选择没有与自己吵过架的男孩有连边(自己的朋友也算,并查集处理),2分图,有些边,求有几种完美匹配(每次匹配每个点都不重复匹配) 我是建二分图后,每次增广一单位,(一次完美匹配 ...
- HDU 3081 Marriage Match II(二分法+最大流量)
HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个 ...
- 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 ...
- HDU 3081 Marriage Match II 二分 + 网络流
Marriage Match II 题意:有n个男生,n个女生,现在有 f 条男生女生是朋友的关系, 现在有 m 条女生女生是朋友的关系, 朋友的朋友是朋友,现在进行 k 轮游戏,每轮游戏都要男生和女 ...
- Marriage Match II(二分+并查集+最大流,好题)
Marriage Match II http://acm.hdu.edu.cn/showproblem.php?pid=3081 Time Limit: 2000/1000 MS (Java/Othe ...
- HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
随机推荐
- 【转】string常用函数
原文地址:http://hi.baidu.com/baowup/blog/item/3a27465c86d71546faf2c066.html/cmtid/de1ef3f0de7554a0a40f52 ...
- wpf Content数据绑定StringFormat起作用的原理和解决(转)
1.简单示例: <Window x:Class="WpfOne.Bind.Bind6" xmlns="http://schemas.microsoft.com/wi ...
- webservice调用接口,接口返回数组类型
1. 其中sendSyncMsg1接口是方法名,Vector实现了List接口,xml是sendSyncMsg1的方法形参 Service service = new Service(); Call ...
- 你好,C++(37)上车的人请买票!6.3.3 用虚函数实现多态
6.3.3 用虚函数实现多态 在理解了面向对象的继承机制之后,我们知道了在大多数情况下派生类是基类的“一种”,就像“学生”是“人”类中的一种一样.既然“学生”是“人”的一种,那么在使用“人”这个概念 ...
- [转]python对json的相关操作
json官方说明参见:http://json.org/ Python操作json的标准api库参考:http://docs.python.org/library/json.html 对简单数据类型的e ...
- Thinkphp---练习:数据的增删改查
利用ThinkPHP连接数据库的增删改查的例题:用到的数据库表名Info表,Nation表 数据显示页面:MainController.class.php中的方法(增删改查全包括--function ...
- DEDE在下载文件时会生成table
当我们在系统内容模型中添加附件类型字段时,前台需要用{dede:field name='字段名'/}来调用. 例如我在后台发布一篇文章,上传一个zip的附件,字段的时间内容是:'/uploads/so ...
- PHP导出MySQL数据到Excel文件
PHP导出MySQL数据到Excel文件 转载 常会碰到需要从数据库中导出数据到Excel文件,用一些开源的类库,比如PHPExcel,确实比较容易实现,但对大量数据的支持很不好,很容易到达PHP内存 ...
- jquery ajax(3).post
.post返回文本数据 $(function(){ $("#send").click(function(){ $.post("post1.php", { use ...
- Linux shell (一)
echo -e "Hello World! \a \n" # -e 解析反斜杠 read -p "Please input your first name: &q ...