hdu3081 Marriage Match II
新年第一篇,又花了一早上,真是蠢啊!
二分+网络流
之前对于讨论哪些人是朋友的时候复杂度过高
直接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的更多相关文章
- HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流
题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others) ...
- hdu3081 Marriage Match II(最大流)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Marriage Match II Time Limit: 2000/1000 M ...
- HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu3081 Marriage Match II(二分+并查集+最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意: n个女生与n个男生配对,每个女生只能配对某些男生,有些女生相互是朋友,每个女生也可以跟她 ...
- 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 (二分图,并查集) Description Presumably, you all have known the question of stab ...
- HDU 3081 Marriage Match II(二分法+最大流量)
HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个 ...
- Marriage Match II(二分+并查集+最大流,好题)
Marriage Match II http://acm.hdu.edu.cn/showproblem.php?pid=3081 Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 3081 Marriage Match II 二分 + 网络流
Marriage Match II 题意:有n个男生,n个女生,现在有 f 条男生女生是朋友的关系, 现在有 m 条女生女生是朋友的关系, 朋友的朋友是朋友,现在进行 k 轮游戏,每轮游戏都要男生和女 ...
随机推荐
- How to fix “HTTP Status Code 505 – HTTP Version Not Supported” error?--转
http://dotnetstock.com/technical/http-status-code-505-http-version-not-supported/ The reason for the ...
- 51NOD 1220 约数之和 [杜教筛]
1220 约数之和 题意:求\(\sum_{i=1}^n \sum_{j=1}^n \sigma_1(ij)\) \[ \sigma_0(ij) = \sum_{x\mid i}\sum_{y\mi ...
- xcode7中使用cocos2d-x3.8的webview控件
在XCode7中使用cocos2d-x 3.3以上版本的WebView控件时,碰到了编译错误 App Transport Security has blocked a cleartext HTTP ( ...
- IOS教程视频汇总
使用StoryBoard做iOS UI界面跳转
- sublime插件AndyJS2安装教程
1.下载AndyJS2包,已整理上传,下载AndyJS2.rar,附上网址.(https://github.com/jiaoxueyan/AndyJS2) 2.点击首选项(preference)=&g ...
- html中meta标签及用法理解
自己一直想成为高级前端开发工程师,而自学.奈何最近感觉自学收效甚微,一度迷茫. 不破不立,打算改变这样的状态. 春节后上班第一天,今年打算好好实现自己的前端梦想. 重新整理.总结前端技术. 废话,就不 ...
- 读书共享 Primer Plus C-part11
第十四章结构和其他数据形式 关于fread以及fwrite fread(char* buff,int size,int count,FILE* fp) fwrite(char* buff,int si ...
- 一步一步配置ABP Core Template with Angular
1.首先去https://aspnetboilerplate.com/Templates下载模板工程,按如下勾选 2.下载后打开工程如下图,并设置Web.host 作为启动项目,rebuild 还原n ...
- 织梦调用seotitle
如果有seotitle则调用seotitle,没有则调用title {dede:field.array runphp='yes'} if(@me['seotitle']=='') {@me=@me[' ...
- iOS实现微信外部H5支付完成后返回原APP
看到微信最近放开了微信H5支付,公司决定把H5集成到多款APP上.下面记录下了开发过程. 由于是微信新推出的支付方式,在网上搜索到的相关资料并不多,其中有一篇文件(点此跳转)对我的整个开发过程起到了很 ...