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 ...
随机推荐
- JavaScript加强之自定义callback示例
callback回调函数在本文以自定义的方式出现. html: <select id="select"> <option value="111& ...
- 写文件前, 检查目录写权限(PHP)
写文件前, 检查目录写权限 写或保存文件前, 确保目录是可写的, 假如不可写, 输出错误信息. 这会节约你很多调试时间. linux系统中, 需要处理权限, 目录权限不当会导致很多很多的问题, 文件也 ...
- RTC搭建android下三层应用程序访问服务器MsSql-服务器端
前几天通过Ro搭建webservice,然后在android下调用,虽然已近成功,但是返回的数据库里的中文有乱码一直未得到解决!rtc6.23版本,已经支持xe5,也支持fmx的android下开发, ...
- Linux tar 解压缩命令
tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个 ...
- MySQL创建数据库[保存mojo格式的数据库]已经常用的utf8格式数据库
一.创建最新编码utf8mb4格式的库: CREATE DATABASE IF NOT EXISTS yourdbname CHARACTER SET utf8mb4 COLLATE utf8mb4_ ...
- asp 回发的时候样式变化
在一个按钮确定后弹出一个提示框,在提示框没有关闭时有时会发现页面的样式发生变化. 解决方法: 在DIV外增加,<table><tr><td align="lef ...
- 3DSoftRenderer
研究了好几天基本的图形学,对于光栅化的大致过程有点了解了,很感谢网上的很多大牛的无私奉献,我就写一下这几天的总结,希望也能对网络上的知识做出一点点点的贡献. 屏幕有什么特点,无非是一排排的像素点,每个 ...
- js中arguments的作用
在javascript函数体内,标识符arguments具有特殊含义.它是调用对象的一个特殊属性,用来引用Arguments对象. Arugments对象就像数组,注意这里只是像并不是哈. javas ...
- easy ui easyui-linkbutton 禁用、启用
<a id="btn_update_Shop" name="btn_update_Shop" class="easyui-linkbutton& ...
- POJ - 1741 Tree
DescriptionGive a tree with n vertices,each edge has a length(positive integer less than 1001).Defin ...