HDU - 3081 Marriage Match II 【二分匹配】
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=3081
题意
有n对男女 女生去选男朋友
如果女生从来没和那个男生吵架 那么那个男生就可以当她男朋友
女生也可以选择从来没和自己闺蜜吵过架的男生当男朋友
如果 女生A和女生B是闺蜜 女生A和男生C吵过架 但是女生B和男生C从来没吵过架
那么女生A是可以选择男生C当男朋友的。
看来 讨好闺蜜是一件多么重要的事情。。
T T组数据
给出 n, m, f
有N对男女, m对从未吵架关系 f 对闺蜜关系
思路
先将m对关系 用二分图存下来
然后f对闺蜜关系 先用并查集并起来 并完后,跑一下Flyod
然后去二分匹配
匹配完一次 将匹配过的边删去 再去匹配,如果还能匹配成功 继续删边 继续匹配 匹配的次数 就是答案
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
#define bug puts("***bug***");
#define fi first
#define se second
//#define bug
//#define gets gets_s
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi;
const double PI = acos(-1.0);
const double EI = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 7e3 + 5e2 + 10;
const int MOD = 6;
const int MAXN = 110;
int uN, vN;//u,v的数目,使用前面必须赋值
int g[MAXN][MAXN];//邻接矩阵
int linker[MAXN];
bool used[MAXN];
bool dfs(int u)
{
for (int v = 0; v < vN; v++)
if (g[u][v] && !used[v])
{
used[v] = true;
if (linker[v] == -1 || dfs(linker[v]))
{
linker[v] = u;
return true;
}
}
return false;
}
int hungary()
{
int res = 0;
memset(linker, -1, sizeof(linker));
for (int u = 0; u < uN; u++)
{
memset(used, false, sizeof(used));
if (dfs(u))res++;
}
return res;
}
int pre[MAXN];
int n, m, f;
int find(int x)
{
while (x != pre[x])
x = pre[x];
return x;
}
void join(int x, int y)
{
int fx = find(x), fy = find(y);
if (fx != fy)
pre[fx] = fy;
}
void Flyod()
{
for (int i = 0; i < n; i++)
{
for (int k = 0; k < n; k++)
{
if (find(i) == find(k))
{
for (int j = 0; j < n; j++)
g[i][j] = g[k][j] = (g[i][j] || g[k][j]);
}
}
}
}
void clear()
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
g[i][j] = 0;
for (int i = 0; i < n; i++)
pre[i] = i;
}
int main()
{
int t;
cin >> t;
while (t--)
{
scanf("%d%d%d", &n, &m, &f);
clear();
int x, y;
for (int i = 0; i < m; i++)
{
scanf("%d%d", &x, &y);
x--, y--;
g[x][y] = 1;
}
for (int i = 0; i < f; i++)
{
scanf("%d%d", &x, &y);
x--, y--;
join(x, y);
}
Flyod();
int tot = 0;
uN = vN = n;
int vis;
while (true)
{
vis = hungary();
if (vis != n)
break;
tot++;
for (int i = 0; i < n; i++)
g[linker[i]][i] = 0;
}
cout << tot << endl;
}
}
HDU - 3081 Marriage Match II 【二分匹配】的更多相关文章
- HDU 3081 Marriage Match II 二分 + 网络流
Marriage Match II 题意:有n个男生,n个女生,现在有 f 条男生女生是朋友的关系, 现在有 m 条女生女生是朋友的关系, 朋友的朋友是朋友,现在进行 k 轮游戏,每轮游戏都要男生和女 ...
- HDU 3081 Marriage Match II (二分+并查集+最大流)
题意:N个boy和N个girl,每个女孩可以和与自己交友集合中的男生配对子;如果两个女孩是朋友,则她们可以和对方交友集合中的男生配对子;如果女生a和女生b是朋友,b和c是朋友,则a和c也是朋友.每一轮 ...
- 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 pid=3081" target="_blank" style="">题目链接 题意:n个 ...
- 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 最大流OR二分匹配
Marriage Match IIHDU - 3081 题目大意:每个女孩子可以和没有与她或者是她的朋友有过争吵的男孩子交男朋友,现在玩一个游戏,每一轮每个女孩子都要交一个新的男朋友,问最多可以玩多少 ...
- HDU 3081 Marriage Match II (二分+网络流+并查集)
注意 这题需要注意的有几点. 首先板子要快,尽量使用带当前弧优化的dinic,这样跑起来不会超时. 使用弧优化的时候,如果源点设置成0,记得将cur数组从0开始更新,因为有的板子并不是. 其次这题是多 ...
- HDU 3081 Marriage Match II
二分图的最大匹配+并查集 每次匹配完之后,删除当前匹配到的边. #include<cstdio> #include<cstring> #include<cmath> ...
- Marriage Match II(二分+并查集+最大流,好题)
Marriage Match II http://acm.hdu.edu.cn/showproblem.php?pid=3081 Time Limit: 2000/1000 MS (Java/Othe ...
随机推荐
- 【Excle】在重复数据中对日期排序并查询最新的一条记录
现在存在以下数据: 需要查询出以下数据 姓名 日期 张三 2017-12-14 李四 2017-12-16 在E1中写入以下公式:=IF(D2=MAX(IF($C$ ...
- Spring Cloud Zuul 网关的分布式系统中整合Swagger(转)和 zuul跨域访问问题
首先恭喜自己终于找对了努力的方向,很荣幸能在公司接触到微服务架构,也很高兴公司一个大佬哥们愿意带我,他技术确实很牛逼,我也很佩服他,前后端通吃,干了六年能有这样的水平.最近跟着在搞微服务架构,给我分配 ...
- bean对应mapper.xml字段
在bean中set的时候最好写上这个,避免报空指针............... public void setImgAddress(String imgAddress) { this.imgAddr ...
- Linux基础之常用基本命令备忘
Linux基础之常用基本命令备忘 PWD 查询当前所在Linux上的位置 / 根目录 CD(change directory)切换目录 语法 CD /(注意添加空格) LS ...
- win10--vs2015--libjpeg--64位库的编译过程记录
win10--vs2015--libjpeg--64位库的编译过程记录 1. 下载源代码: http://libjpeg.sourceforge.net/ 或者 http://www.ij ...
- erlang的md5加密
二话不说,直接上代码 -module(md5). -compile(export_all). md5(S) -> Md5_bin = erlang:md5(S), Md5_list = bina ...
- Python修饰器的函数式编程(转)
From:http://coolshell.cn/articles/11265.html 作者:陈皓 Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Desi ...
- CSDN--字体颜色--markdown
在写blog时,想高亮某些字,但是发现markdown更改字体颜色不像word里那么方便,于是查了一下,要用一下代码进行更改字体颜色,还可以更改字体大小,还有字体格式 <font 更改语法> ...
- shell 遍历所有文件包括子目录
1.代码简单,但是难在校验,不像python那么好理解 建议在Notepad++下编辑. 2.注意引用linux命令的`是[tab]键上面那个 3.if[] 这里 Error : syntax er ...
- 使用Eclipse自带的Maven插件创建Web项目时报错:
问题描述: 使用Eclipse自带的Maven插件创建Web项目时报错: Could not resolve archetype org.apache.maven.archetypes:maven-a ...