题目大意:有一些男孩和女孩玩一个游戏,每个女孩都可以挑一个男孩来进行这个游戏(所有人都要参加),女孩只会挑选她喜欢的男孩,并且她们认为她们朋友喜欢的男孩她们也是喜欢的(朋友的男朋友也是我的男朋友???),而且她们遵循朋友的朋友也是朋友的原则,问她们最多可以玩几轮游戏(每轮要选择的人不能和以前选择的相同)。
 
分析:朋友关系很明显可以使用并查集找出来每个女孩可以连接的男孩,因为要求的是最多能进行多少轮游戏,也就是在这x轮游戏中每个女孩换了x不同的男孩,每个男孩也换了x个不同的女孩,如果源点和女孩相连,汇点和男孩相连,那么流量一定是N*x,可以使用二分来查找最大的x。
下面是AC代码。
=========================================================================================================================
#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(最大流)的更多相关文章

  1. Marriage Match II HDU - 3081(二分权值建边)

    题意: 有编号为1~n的女生和1~n的男生配对 首先输入m组,a,b表示编号为a的女生没有和编号为b的男生吵过架 然后输入f组,c,d表示编号为c的女生和编号为d的女生是朋友 进行配对的要求满足其一即 ...

  2. 【HDU3081】Marriage Match II (二分+最大流)

    Description Presumably, you all have known the question of stable marriage match. A girl will choose ...

  3. hdu 3081 hdu 3277 hdu 3416 Marriage Match II III IV //最大流的灵活运用

    3081 题意: n个女孩选择没有与自己吵过架的男孩有连边(自己的朋友也算,并查集处理),2分图,有些边,求有几种完美匹配(每次匹配每个点都不重复匹配) 我是建二分图后,每次增广一单位,(一次完美匹配 ...

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

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

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

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

  6. HDU 3081 Marriage Match II (二分图,并查集)

    HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...

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

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

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

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

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

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

随机推荐

  1. yii2 控制器里 action 大小写组合造成的路由问题

    yii1中, 若存在如下控制器 class BindController extends CController { public function actionGetMobilePhone () { ...

  2. ubuntu 修改运行级别

    只转载了成功的, 具体参见原文  http://www.2cto.com/os/201308/237632.html 第一种方法:(内核级别的)   Sudo vi /etc/default/grub ...

  3. Day3 - Python基础3 函数、递归、内置函数

    Python之路,Day3 - Python基础3   本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8. ...

  4. Tips--怎么使用谷歌搜索

    修改hosts即可: hosts在哪? windows下:C:\Windows\System32\drivers\etc 管理员身份打开,并将下载好的hosts文件内容,添加到原有的hosts文件末尾 ...

  5. 解决ASP.NET中ReportView与IE11的兼容性问题

    前久发现以前用ReportView开发的一个软件的报表,在IE11上运行时出错,陆续查了好几天才解决了问题. 开发环境: VS2010,ReportView 10.0.402,RDLC报表模板 问题: ...

  6. WinForm窗体之间传值

    当程序需要将一个窗体中的一些信息传给另一个窗体并让其使用时,就需要用到这个知识点 方法一:通过接受参数的窗体的构造函数传值 例:现有Form1和Form2两个窗体,二者都包含一个文本框,Form1还包 ...

  7. java 安卓开发之文件的读与写

    java文件的读与写,代码: String file="user.txt"; private void writeFileData(String str1, String str2 ...

  8. spring web flow 2.0入门(转)

    Spring Web Flow 2.0 入门 一.Spring Web Flow 入门demo(一)简单页面跳转 附源码(转) 二.Spring Web Flow 入门demo(二)与业务结合 附源码 ...

  9. [转]Python中的with…as…

    先说明一个常见问题,文件打开: 1 2 3 4 5 6 7 try:     f = open('xxx')     do something except:     do something fin ...

  10. 关闭iOS的自动更新

    Safari打开网址https://oldcat.me/web/NOOTA9.mobileconfig,安装描述文件,就不会自动下载和提示更新最新的iOS了