Marriage Match II

题意:有n个男生,n个女生,现在有 f 条男生女生是朋友的关系, 现在有 m 条女生女生是朋友的关系, 朋友的朋友是朋友,现在进行 k 轮游戏,每轮游戏都要男生和女生配对,每轮配对过的人在接下来中都不能配对,求这个k最大是多少。

题解:二分 + 网络流check 。

代码:

 #include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb emplace_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = (int)1e9+;
const int N = ;
const int M = N * N;
int head[N], deep[N], cur[N];
int w[M], to[M], nx[M];
int tot;
void add(int u, int v, int val){
w[tot] = val; to[tot] = v;
nx[tot] = head[u]; head[u] = tot++; w[tot] = ; to[tot] = u;
nx[tot] = head[v]; head[v] = tot++;
}
int bfs(int s, int t){
queue<int> q;
memset(deep, , sizeof(deep));
q.push(s);
deep[s] = ;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = nx[i]){
if(w[i] > && deep[to[i]] == ){
deep[to[i]] = deep[u] + ;
q.push(to[i]);
}
}
}
return deep[t] > ;
}
int Dfs(int u, int t, int flow){
if(u == t) return flow;
for(int &i = cur[u]; ~i; i = nx[i]){
if(deep[u]+ == deep[to[i]] && w[i] > ){
int di = Dfs(to[i], t, min(w[i], flow));
if(di > ){
w[i] -= di, w[i^] += di;
return di;
}
}
}
return ;
} int Dinic(int s, int t){
int ans = , tmp;
while(bfs(s, t)){
for(int i = ; i <= t; i++) cur[i] = head[i];
while(tmp = Dfs(s, t, inf)) ans += tmp;
}
return ans;
} void init(){
memset(head, -, sizeof(head));
tot = ;
}
int pre[N];
int link[N][N];
int edge[M][];
int Find(int x){
if(x == pre[x]) return x;
return pre[x] = Find(pre[x]);
}
int T, n, m, f, s, t, u, v;
void GG(int val){
for(int i = head[s]; ~i; i = nx[i]){
if(i&);
else w[i] = val, w[i+] = ;
}
for(int i = n+; i <= n+n; i++){
for(int j = head[i]; ~j; j = nx[j]){
if(j&);
else w[j] = val, w[j+] = ;
}
}
for(int i = ; i <= n; i++){
for(int j = head[i]; ~j; j = nx[j]){
if(j&);
else w[j] = , w[j+] = ;
}
}
}
int main(){
scanf("%d", &T);
while(T--){
init();
scanf("%d%d%d", &n, &m, &f);
for(int i = ; i <= n; i++){
pre[i] = i;
for(int j = ; j <= n; j++)
link[i][j] = ;
}
for(int i = ; i <= m; i++)
scanf("%d%d", &edge[i][], &edge[i][]);
for(int i = ; i <= f; i++){
scanf("%d%d", &u, &v);
u = Find(u);
v = Find(v);
if(v == u) continue;
pre[u] = v;
}
for(int i = ; i <= m; i++){
int z = Find(edge[i][]);
link[z][edge[i][]] = ;
}
s = , t = * n + ;
for(int i = ; i <= n; i++){
add(s, i, );
int z = Find(i);
for(int j = ; j <= n; j++){
if(link[z][j])
add(i, j+n, );
}
add(i+n,t,);
}
int l = , r = n;
while(l <= r){
int mid = l+r >> ;
GG(mid);
if(Dinic(s,t) == mid*n) l = mid + ;
else r = mid - ;
}
printf("%d\n", l-);
}
return ;
}

HDU 3081 Marriage Match II 二分 + 网络流的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. HDU - 3081 Marriage Match II 【二分匹配】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意 有n对男女 女生去选男朋友 如果女生从来没和那个男生吵架 那么那个男生就可以当她男朋友 女 ...

  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. Linux系统管理----账号与权限管理作业习题

    1.创建/guanli 目录,在/guanli下创建zonghe 和 jishu 两个目录(一条命令) 创建目录:mkdir +目录 [root@localhost chen]#mkdir /guan ...

  2. TCP queue 的一些问题

    转自Jasey Wang的blog,原文地址 首先回顾下三次握手里面涉及到的问题: 当 client 通过 connect 向 server 发出 SYN 包时,client 会维护一个 socket ...

  3. 聊一聊Java的枚举enum

    一. 什么是枚举 枚举是一种数据类型,具有集合的一些特点,可以存放多个元素,但存储对象有限且固定,枚举也有比较常见的使用场景,如我们需要表达性别(男.女),颜色(红.黄.蓝),星期(星期一.星期二.. ...

  4. Github上fork的项目如何merge原Git项目

    问题场景 小明在Github上fork了一个大佬的项目,并clone到本地开发一段时间,再提交merge request到原Git项目,过了段时间,原作者联系小明,扔给他下面这幅截图并告知合并处理冲突 ...

  5. 用多线程优化Excel表格数据导入校验的接口

    公司的需求,当前某个Excel导入功能,流程是:读取Excel数据,传入后台校验每一条数据,判断是否符合导入要求,返回给前端,导入预览展示.(前端等待响应,难点).用户再点击导入按钮,进行异步导入(前 ...

  6. ThreadPoolExecutor线程池的一个面试题

    问题:现有一个线程池,参数corePoolSize = 5,maximumPoolSize = 10,BlockingQueue阻塞队列长度为5,此时有4个任务同时进来,问:线程池会创建几条线程? 如 ...

  7. Java下载文件方法

    public static void download(String path, HttpServletResponse response) { try { // path是指欲下载的文件的路径. F ...

  8. http测试工具

    http测试工具: https://github.com/denji/awesome-http-benchmark wrk https://github.com/wg/wrk wrk2 https:/ ...

  9. Mermaid

    graph TD; A-->B; A-->C; B-->D; C-->D;

  10. 简单设计企业级JOB平台

    前言 在企业级项目中有许多能够用到定时任务的场景例如: 在某个时间点统一给某些用户发送邮件信息 接口表数据发送 某月某日更新报表数据 ...... 目前我们使用SpringBoot快速整合Quartz ...