HDU 3081 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 二分 + 网络流的更多相关文章
- 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 【二分匹配】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意 有n对男女 女生去选男朋友 如果女生从来没和那个男生吵架 那么那个男生就可以当她男朋友 女 ...
- 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 ...
随机推荐
- 虚IP解决程序连只读服务器故障漂移
目前公司有一套核心交易数据库配置了AlWaysON,SQL 2012版本, 1主4从, 其从库(8,14, 8.15) 这2台只读的从数据库服务器, 后台程序和wms等很多程序,都是直接配置IP连接这 ...
- JavaScript的event对象
JavaScript的event对象中 event.target指代的是:触发事件的元素 event.currentTarget指代的是:事件绑定的元素 <!DOCTYPE html> & ...
- 利用反射,更改string对象的value数组以达到改变string值。
package test; import java.lang.reflect.Field; import lombok.Value; public class Test1{ public static ...
- Netty学习(五)-DelimiterBasedFrameDecoder
上一节我们说了LineBasedframeDecoder来解决粘包拆包的问题,TCP以流的方式进行数据传输,上层应用协议为了对消息进行区分,一般采用如下4种方式: 消息长度固定,累计读取到消息长度总和 ...
- 牛客多校训练第八场G.Gemstones(栈模拟)
题目传送门 题意: 输入一段字符串,字符串中连续的三个相同的字符可以消去,消去后剩下的左右两段字符串拼接,求最多可消去次数. 输入:ATCCCTTG 输出:2 ATCCCTTG(消去CCC)——& ...
- react-native 入门基础介绍
目录 安装 项目 主要目录结构 入口 Home模块 Coobook模块 List模块 novel模块 相关参考 一个简单的demo,用于介绍react-native相关基础信息,主要是针对有兴趣的同学 ...
- ecshop 管理后台菜单及权限管理机制
ecshop 所有的一级菜单选项存放于languages\zh_cn\admin\common.php 文件里面,使用 $_LANG['02_cat_and_goods'] = '商品管理'; 这样 ...
- (一)c#Winform自定义控件-基类控件
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- forward(转发)和redirect(重定向)的区别
在学习Servlet和JSP时,经常会使用到forward和redirect,我们先来看这两者在Servlet中的调用方式: 1.forward request.getRequestDispatche ...
- Ubuntu18设置mysql的sql_mode
原因: MySQL 5.7.5及以上功能依赖检测功能.如果启用了ONLY_FULL_GROUP_BY SQL模式(默认情况下),MySQL将拒绝选择列表,HAVING条件或ORDER BY列表的查询引 ...