题目链接

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. 关于其它模块的设计,有非常多须要自己去构建和完毕,在这里就简单地举几个样例来看看其它模块的设计。我们要做的就是有更改password模块,客户选择模块和关于本软件模块。更改password模块用来更改管理员的password,客户选择对话框模块用来选择已加入的客户,关于本软件模块用来说明客户管理系统的一些必要信息和制作人的信息。

            五,其它模块设计         关于其它模块的设计,有非常多须要自己去构建和完毕,在这里就简单地举几个样例来看看其它模块的设计. 我们要做的就是有更改password模块.客户选择模 ...

  2. jq:jq开头为什么那么写

    转自:http://holysonll.blog.163.com/blog/static/2141390932013411112823855/ 用jQ的人很多人都是这么开始写脚本的: $(functi ...

  3. .aspx 页面引用命名空间

    一.单个页面引用: <%@ Import Namespace="" %> 二.所有页面引用,Web.config配置如下: <system.web> < ...

  4. 后期给项目加入Git版本控制

    一.为项目加上Git 1.进入对应文件夹 2.git init(初始化一个空的代码仓库) 3.git add .(将当前目录和子目录的文件标记为要添加到代码仓库) 4.git commit -m &q ...

  5. apktool反编译时各种问题汇总

    问题1:apktool d -d 时出现错误Error occured while disassembling class办法:这不是你的错误,这是apktool本身的错误,目前正式release的1 ...

  6. Resharper快捷键及用法

    建议你使用 Reshaper 的快捷键,不要担心 Reshaper 会把你原来的快捷键设置给覆盖了,因为如果某个快捷键和 VS 是冲突的,Reshaper会让你自己选择需要使用 VS 还是 Resha ...

  7. python之开篇---hello world!

    (1)前沿 (2)python 简介 (3)python hello world 实现 (4) -------------qq:1327706646 ------------------------- ...

  8. chef简介

    Chef 的简单介绍 Chef 主要分为三个部分 Chef Server.Workstation 以及 Chef Client.用户在 Workstation 上编写 Cookbook.然后,通过 k ...

  9. Linux进程间通信(五) - 信号灯(史上最全)及其经典应用案例

    信号灯概述 什么是信号灯 信号灯用来实现同步,用于多线程,多进程之间同步共享资源(临界资源). PV原语:信号灯使用PV原语 P原语操作的动作是: u  sem减1. u  sem减1后仍大于或等于零 ...

  10. Mysql字符串截取函数

    今天建视图时,用到了MySQL中的字符串截取,很是方便. 感觉上MySQL的字符串函数截取字符,比用程序截取(如PHP或JAVA)来得强大,所以在这里做一个记录,希望对大家有用. 函数: 1.从左开始 ...