uva753 A Plug for UNIX
最大流。
流可以对应一种分配方式。
显然最大流就可以表示最多匹配数
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 500 + 10;
const int maxm = 100000 + 10;
const int maxl = 30;
const int inf = 0x3f3f3f3f; char s[maxn][maxl],t[maxl],t2[maxl];
int g[maxn],v[maxm],nex[maxm],f[maxm],eid;
int id[maxn],vid;
int S,T;
int d[maxn],gap[maxn];
int n,m,k,a1,a2; void addedge(int a,int b,int F) {
v[eid]=b; f[eid]=F; nex[eid]=g[a]; g[a]=eid++;
v[eid]=a; f[eid]=0; nex[eid]=g[b]; g[b]=eid++;
} void build() {
memset(g,-1,sizeof(g)); eid=0;
vid=0;
S=++vid; T=++vid;
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%s",s[i]);
id[i]=0;
for(int j=1;j<i;j++) if(strcmp(s[i],s[j])==0) {
id[i]=id[j];
break;
}
if(!id[i]) id[i]=++vid;
addedge(id[i],T,1);
//printf("id[%d]=%d\n",i,id[i]);
}
scanf("%d",&m);
for(int i=n+1;i<=n+m;i++) {
scanf("%s",t);
scanf("%s",s[i]);
id[i]=0;
for(int j=1;j<i;j++) if(strcmp(s[i],s[j])==0) {
id[i]=id[j];
break;
}
if(!id[i]) id[i]=++vid;
addedge(S,id[i],1);
//printf("id[%d]=%d\n",i,id[i]);
}
scanf("%d",&k);
for(int i=1;i<=k;i++) {
scanf("%s",t);
scanf("%s",t2);
for(int j=1;j<=n+m;j++) if(strcmp(s[j],t)==0) {a1=id[j]; break;}
for(int j=1;j<=n+m;j++) if(strcmp(s[j],t2)==0) {a2=id[j]; break;}
addedge(a1,a2,inf);
//printf("%d %d\n",a1,a2);
}
} int ISAP(int u,int flow) {
if(u==T) return flow;
int cur=0,aug,mindist=vid; for(int i=g[u];~i;i=nex[i])
if(f[i] && d[v[i]]+1==d[u]) {
aug=ISAP(v[i],min(flow-cur,f[i]));
cur+=aug;
f[i]-=aug;
f[i^1]+=aug;
if(cur==flow || d[S]>=vid) {
return cur;
}
} if(cur==0) {
if(!--gap[d[u]]) {
d[S]=vid;
return cur;
}
for(int i=g[u];~i;i=nex[i]) if(f[i])
mindist=min(d[v[i]],mindist);
++gap[d[u]=mindist+1];
}
return cur;
} void solve() {
memset(d,0,sizeof(d));
memset(gap,0,sizeof(gap));
int res=0;
gap[0]=vid;
while(d[S]<vid) {
res+=ISAP(S,inf); }
printf("%d\n",m-res);
} int main() {
int T;
scanf("%d",&T);
while(T--) {
build();
solve();
if(T) printf("\n");
}
return 0;
}
uva753 A Plug for UNIX的更多相关文章
- uva753 A Plug for UNIX 网络流最大流
C - A Plug for UNIX You are in charge of setting up the press room for the inaugural meeting of t ...
- UVa753/POJ1087_A Plug for UNIX(网络流最大流)(小白书图论专题)
解题报告 题意: n个插头m个设备k种转换器.求有多少设备无法插入. 思路: 定义源点和汇点,源点和设备相连,容量为1. 汇点和插头相连,容量也为1. 插头和设备相连,容量也为1. 可转换插头相连,容 ...
- 【poj1087/uva753】A Plug for UNIX(最大流)
A Plug for UNIX Description You are in charge of setting up the press room for the inaugural meeti ...
- POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for UNIX / UVAlive 5418 A Plug for UNIX / SCU 1671 A Plug for UNIX (网络流)
POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for ...
- UVA 753 A Plug for UNIX(二分图匹配)
A Plug for UNIX You are in charge of setting up the press room for the inaugural meeting of the Unit ...
- A Plug for UNIX 分类: POJ 图论 函数 2015-08-10 14:18 2人阅读 评论(0) 收藏
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14786 Accepted: 4994 Desc ...
- POJ1087 A Plug for UNIX(网络流)
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total S ...
- poj 1087 C - A Plug for UNIX 网络流最大流
C - A Plug for UNIXTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...
- UVA 753 - A Plug for UNIX(网络流)
A Plug for UNIX You are in charge of setting up the press room for the inaugural meeting of the U ...
随机推荐
- SharedPreference 的存取
1.通过名称来获取指定的SharedPreferences,下面这句代码表示获取名字问hello的SharedPreferences,数据保存在 data/data/package名/shared_p ...
- psp系统需求分析
软件开发方向“PSP系统”软件需求规约 目录 1 引言... 4 1.1 目的... 4 1.2 文档格式... 4 1.3 预期的读者和阅读建议... 4 1.4 范围... 5 1.5 术语... ...
- c#中sqlhelper类的编写(一)
在.net平台的项目开发中,凡是用到数据库交互的,都有必要了解SqlHelper类的原理. 步骤一: 我就拿WPF项目开发作为例子.首先要新建一个App.config(应用程序配置文件).注意,在VS ...
- java中ReentrantReadWriteLock读写锁的使用
Lock比传统线程模型中的synchronized方式更加面向对象,与生活中的锁类似,锁本身也应该是一个对象.两个线程执行的代码片段要实现同步互斥的效果,它们必须用同一个Lock对象. 读写锁:分为读 ...
- WPF多线程演示
WPF中的几种处理线程的工作方式: 1.简单的DispatcherTimer类似Timer控件 2.需要处理UI同步时,Dispatcher DispatcherOpertion 3.增强的Threa ...
- Python for 循环 失效
昨天发现一个负责处理观察者模式的基类工作失败,默认的N个观察者负责处理 发送的一些东西, 其中提供一个内置接口移除观察者: def removeObserver(self, observer): if ...
- 区间型动规--石子归并(Pascal)
题目描述 Description 有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合并相邻的两堆石子,一次合并的代价为两堆石子的重量和w[i]+w[i+1].问安排怎样的合并顺序,能够使 ...
- 让<未将对象引用到实例>见鬼去吧!
未将对象引用到实例,即NullReferenceException异常,我相信这是c#编程中最常见的错误之一,至少我在做项目的过程中,有很多时候都会抛出这个异常.每当这个异常出现的时候,我都会头皮一紧 ...
- 1194: [HNOI2006]潘多拉的盒子 - BZOJ
Description Input 第一行是一个正整数S,表示宝盒上咒语机的个数,(1≤S≤50).文件以下分为S块,每一块描述一个咒语机,按照咒语机0,咒语机1„„咒语机S-1的顺序描述.每一块的 ...
- no appropriate service handler found The Connection descriptor used by the client was: localhost:1521:myorcl
参考网址:http://www.2cto.com/database/201205/133542.html http://www.cnblogs.com/kerrycode/p/4244493.html ...