题目链接

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 【二分匹配】的更多相关文章

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

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

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

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

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

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

  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 of stab ...

  6. HDU 3081 Marriage Match II 最大流OR二分匹配

    Marriage Match IIHDU - 3081 题目大意:每个女孩子可以和没有与她或者是她的朋友有过争吵的男孩子交男朋友,现在玩一个游戏,每一轮每个女孩子都要交一个新的男朋友,问最多可以玩多少 ...

  7. HDU 3081 Marriage Match II (二分+网络流+并查集)

    注意 这题需要注意的有几点. 首先板子要快,尽量使用带当前弧优化的dinic,这样跑起来不会超时. 使用弧优化的时候,如果源点设置成0,记得将cur数组从0开始更新,因为有的板子并不是. 其次这题是多 ...

  8. HDU 3081 Marriage Match II

    二分图的最大匹配+并查集 每次匹配完之后,删除当前匹配到的边. #include<cstdio> #include<cstring> #include<cmath> ...

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

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

随机推荐

  1. LeetCode-Minimum Window Substring -- 窗口问题

    题目描述 Given a string S and a string T, find the minimum window in S which will contain all the charac ...

  2. mysql 杀掉(kill) lock进程脚本

    杀掉lock进程最快的方法是重启mysql,像你这种情况,1000多sql锁住了,最好是重启如果不允许重启,我提供一个shell脚本,生成 kill id命令杀掉lock线程,如下:--------- ...

  3. 程序员不修复BUG怎么办

    在测试过程中,难免遇到开发人员因为一些原因不想修改个别bug的情况.遇到这种问题时,该如何去推进开发修改bug呢? 一.现状分析 1.开发人员为啥不愿意修复BUG? (1)开发与测试对bug的定义理解 ...

  4. mngoDB 常用语法

    http://topmanopensource.iteye.com/blog/1278812### 连接写法:[IP地址:端口号] mongo 192.168.1.161:27017; show db ...

  5. Android提供支持不同屏幕大小的各种方法

    1 http://blog.csdn.net/guolin_blog/article/details/8830286  (手机平板,通过large-layout来区分两条布局文件) 2 http:// ...

  6. Android应用程序开发以及背后的设计思想深度剖析

    1 http://www.uml.org.cn/mobiledev/201211063.asp 2 ...

  7. thinkphp5.0极速搭建restful风格接口层实例

    作为国内最流行的php框架thinkphp,很快就会发布v5.0正式版了,现在还是rc4版本,但已经很强大了下面是基于ThinkPHP V5.0 RC4框架,以restful风格完成的新闻查询(get ...

  8. Maven的pom文件内容详细理解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  9. llinux获取系统时间

    linux中获取当前时间.统计程序运行时间,可以使用gettimeofday()得到毫秒级的时间统计,利用rdtsc指令获取纳秒级时间统计. gettimeofday() 它是一个linux C库函数 ...

  10. oracle分区表和分区索引概述

    ㈠ 分区表技术概述            ⑴ Range 分区            ① 例子                  create table t         (...列定义...)  ...