新年第一篇,又花了一早上,真是蠢啊!
二分+网络流
之前对于讨论哪些人是朋友的时候复杂度过高
直接n3的暴力虽然看起来复杂度高,其实并不是每次都成立

#include<bits/stdc++.h>
using namespace std;
const int N = 205;
const int INF = 0x3f3f3f3f;
#define sz(X) ((int)X.size()) int A[N], B[N];
int mp[N][N];
int f[N];
int find(int x) { return f[x] == x? x : f[x] = find(f[x]); }
/***********Dinic*********/
int s, t;
struct Edge {
int from, to, cap, flow, nx;
Edge(int a=0, int b=0, int c=0, int d=0, int e=0):from(a), to(b), cap(c), flow(d),nx(e){}
}E[N*N];
int head[N], tot;
bool vis[N];
int d[N];
int cur[N];
void init() {
tot = 0; memset(head,-1,sizeof(head));
}
void add(int from, int to, int cap) {
E[tot] = Edge(from,to,cap,0,head[from]); head[from] = tot++;
E[tot] = Edge(to,from,0,0,head[to]); head[to] = tot++;
}
bool bfs() {
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
d[s] = 0;
vis[s] = 1;
while(!Q.empty()) {
int x = Q.front(); Q.pop();
for(int i = head[x]; ~i; i = E[i].nx) {
Edge& e = E[i];
if(!vis[e.to] && e.cap > e.flow) {
vis[e.to] = 1;
d[e.to] = d[x] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x, int a) {
if(x == t || a == 0) return a;
int flow = 0, f;
for(int& i = cur[x]; ~i; i = E[i].nx) {
Edge& e = E[i];
if(d[x]+1 == d[e.to] && (f = dfs(e.to, min(a, e.cap-e.flow))) > 0) {
e.flow += f;
E[i^1].flow -= f;
flow += f;
a -= f; if(a == 0) break;
}
}
return flow;
}
int Maxflow(int start, int ed) {
s = start; t = ed;
int flow = 0;
while(bfs()) {
for(int i = s; i <= t; ++i) cur[i] = head[i];
flow += dfs(s, INF);
}
return flow;
} int main() {
int n,m,l;
int _; scanf("%d",&_);
while(_--) {
memset(mp,0,sizeof(mp));
scanf("%d %d %d",&n,&m,&l);
for(int i = 0; i <= n; ++i) f[i] = i;
for(int i = 0; i < m; ++i) {
int a, b; scanf("%d %d",&a,&b);
mp[a][b] = 1;
}
for(int i = 0; i < l; ++i) {
int c,d; scanf("%d %d",&c,&d);
int t1 = find(c); int t2 = find(d);
if(t1 != t2) f[t1] = t2;
}
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j) {
if(find(i) == find(j)) {
for(int k = 1; k <= n; ++k) {
if(mp[i][k]) mp[j][k] = 1;
}
}
}
}
int s = 0; int t = 2*n+1; int l = 0; int r = n; int res;
while(l <= r) {
int mid = (l + r)>>1;
init();
for(int i = 1; i <= n; ++i) add(s,i,mid);
for(int i = 1; i <= n; ++i) add(i+n,t,mid);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j) {
if(mp[i][j]) {
add(i,j+n, 1);
}
}
int tt = Maxflow(s, t);
if(tt != mid*n) r = mid-1;
else { l = mid+1; res = mid; }
}
printf("%d\n",res); }
return 0;
}

hdu3081 Marriage Match II的更多相关文章

  1. HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流

    题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    ...

  2. hdu3081 Marriage Match II(最大流)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Marriage Match II Time Limit: 2000/1000 M ...

  3. HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))

    Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. hdu3081 Marriage Match II(二分+并查集+最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意: n个女生与n个男生配对,每个女生只能配对某些男生,有些女生相互是朋友,每个女生也可以跟她 ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. UOJ #207. 共价大爷游长沙 [lct 异或]

    #207. 共价大爷游长沙 题意:一棵树,支持加边删边,加入点对,删除点对,询问所有点对是否经过一条边 一开始一直想在边权上做文章,或者从连通分量角度考虑,比较接近正解了,但是没想到给点对分配权值所以 ...

  2. Linux 下编写服务器程序时关于Address already in use 的小错误

    新手,,学习linux服务器编程的时候,bind()函数出现了Address already in use 的错误,这是因为上一次bind过后,还未释放,,只要在socket和bind之间加一个函数就 ...

  3. 对.Net Core结合Docker和Jexus的实践

    本文基于上次尝试之后的进一步尝试,加入Docker容器.编写Dockerfile,并且jexus结合Docker的使用,总结下自己的个人感想. 一.环境介绍 当前的场景有两种方式将Demo实现运行,一 ...

  4. chrome无法登陆账号,显示操作超时的解决方案

    起因 今天重装了下windows操作系统,准备登陆chrome浏览器,以同步各种插件(你懂的),结果是...无法登陆账号,显示操作超时,真是无语了. 碰到了这个问题第一个直觉是:FQ.突然想到如果修改 ...

  5. 为Android添加JNI支持

    起因 今天在进行Android原生开发时,需要通过JNI调用C++代码实现一些处理.以前没有做过类似的东西,在网上找了很久才解决问题,特记录下来以便以后翻阅. Eclipse无cygwin编译so的方 ...

  6. 自兴人工智能------------python入门基础(2)列表和元祖

    一.通用序列操作: 列表中所有序列都可以进行特定的操作,包括索引(indexing).分片(slicing).序列相加(adding).乘法,成员资格,长度,最小值,最大值,下面会一一介绍这些操作法. ...

  7. PLECS—晶闸管-第九周

    1. 单相桥式晶闸管整流电路仿真 (1)仿真电路图 (2)触发角为pi/4的手工波形图(参数设置,触发角=pi/4, 电感L = 0H) (2)模拟仿真波形图 1)参数设置:触发角=pi/4, 电感L ...

  8. GitHub入门与实践

    基本命令 git status 工作区状态 git add git commint 暂存区 git push gitHub客户端 下载网址:https://desktop.github.com/ 解决 ...

  9. python自动化--语言基础1--数据类型及类型转换

    Python中核心的数据类型有哪些?变量(数字.字符串.元组.列表.字典) 什么是数据的不可变性?哪些数据类型具有不可变性数据的不可变是指数据不可更改,比如: a = ("abc" ...

  10. Java经典编程题50道之二十七

    求100之内的素数. public class Example27 {    public static void main(String[] args) {        prime();    } ...