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. 【Spring】No converter found for return value of type: class java.util.ArrayList

    错误信息: org.springframework.http.converter.HttpMessageNotWritableException: No converter found for ret ...

  2. 一看就懂的K近邻算法(KNN),K-D树,并实现手写数字识别!

    1. 什么是KNN 1.1 KNN的通俗解释 何谓K近邻算法,即K-Nearest Neighbor algorithm,简称KNN算法,单从名字来猜想,可以简单粗暴的认为是:K个最近的邻居,当K=1 ...

  3. poj 3714 寻找最近点对

    参考自<编程之美>169页,大概原理就是把区间分成两部分,然后递归找每一部分中最近的点对,还有一种情况就是这个点对分属于这两部分,然后选两部分中的部分点枚举即可,取其最小值. //2013 ...

  4. 用python绘制漂亮的图形

    先看效果,没有用任何绘图工具,只是运行了一段python代码. 代码如下: _ = ( 255, lambda V ,B,c :c and Y(V*V+B,B, c -1)if(abs(V)<6 ...

  5. weblogic 内存溢出解决 java.lang.OutOfMemoryError: PermGen space

    解决办法: 1.在idea中,运行时给weblogic server中 VM options 配置增加内存的参数:-server -XX:PermSize=1024m -XX:MaxPermSize= ...

  6. java常见面试题目(三)

    1.jsp的内置对象. JSP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.config.page. ...

  7. 理解MySQL(二)--数据库事务

    1.事务:事务内的语句,要么全部执行成功,要么全部执行失败. a)      数据库事务四要素:ACID,原子性,一致性,隔离性,持久性. b)      原子性:一个事务必须被视为不可分割的最小单元 ...

  8. 从SpringBoot构建十万博文聊聊缓存穿透

    前言 在博客系统中,为了提升响应速度,加入了 Redis 缓存,把文章主键 ID 作为 key 值去缓存查询,如果不存在对应的 value,就去数据库中查找 .这个时候,如果请求的并发量很大,就会对后 ...

  9. django的安装及基本设置记录

    环境变量的配置在这个文章中,不会的可以去看看 https://www.cnblogs.com/alex3174/p/11116558.html 主要步骤是:我的电脑-右键-属性-高级系统设置-环境变量 ...

  10. h5微信浏览器复制粘贴--ios兼容问题的解决方法(clipboard.js插件)

    前段时间在做微信h5的时候,遇到了ios兼容,使用clipboard.js插件完美解决 下载地址:下载地址: https://github.com/zenorocha/clipboard.js cnd ...