新年第一篇,又花了一早上,真是蠢啊!
二分+网络流
之前对于讨论哪些人是朋友的时候复杂度过高
直接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. Java多线程基础(二)

    信号量Semaphore,类似于锁的功能,用于多线程中对一组资源的控制. acquire方法用于尝试获取一个资源,未获取前将一直等待.release用于释放一个资源,release的前提是已经获得了一 ...

  2. Ling && Lambda

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. Python中Template使用的一个小技巧

    Python中Template是string中的一个类,可以将字符串的格式固定下来,重复利用. from string import Template s = Template("there ...

  4. web攻击

    一.XSS(跨站脚本攻击) 最常见和基本的攻击WEB网站的方法.攻击者在网页上发布包含攻击性代码的数据.当浏览者看到此网页时,特定的脚本就会以浏览者用户的身份和权限来执行.通过XSS可以比较容易地修改 ...

  5. qt窗口的切换

    思想:在一个窗口类中声明另一继承与Qdialog的类的变量 还有在另一类中parentwidget()函数获取父类窗口,然后将其隐藏.. 窗口1: mywin1.h #ifndef MYWIN1_H ...

  6. php+redis 学习 二 悲观锁

    <?php header('content-type:text/html;chaeset=utf-8'); /** * redis实战 * * 实现悲观锁机制 * */ $timeout = 5 ...

  7. Spring Boot让开发如此简单

    从html到asp后一直专注.net开发,从.net诞生到如今,从winform到webform,从asp.net到.net mcv,从.net mvc到.net core,从ado.net到linq ...

  8. ThinkPHP的基本操作

    一.生成入口文件 1.打开服务器,在本地环境测试时在地址栏输入localhost/项目文件名/index.php  可以在Application下面生成一个home模块,记得在这之前,要建立一个项目文 ...

  9. System.in实现数据的键盘输入

    System.in The "standard" input stream. This stream is already open and ready to supply inp ...

  10. 在Linux/Centos下用wondershaper限速

    wondershaper是国外人开发的一款在Linux内核下基于TC工具的对整块网卡的限度工具,虽然有很久没有更新了,但是测试老版本在Centos6.3上依然可以使用. 首先下载wondershape ...