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 ...
随机推荐
- 用AJAX自定义日历
需求分析 在一些购物网站中,都会有促销活动,这些活动都在日历上标注出来,如何通过Ajax让日历 通过读取数据库中的信息,正确的把促销活动标注在日历上,本文通过自定义日历来实现这 个问题. 技术难点 日 ...
- 淘宝IP地址库采集器c#代码
这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下. 最近做一个项目,功能类似于CNZZ站长统计功能,要求显示Ip所在的省份市区/提供商等信息.网上的Ip纯真数据库,下载下来一看 ...
- Linux编程学习笔记 -- Process
进程是一个程序的运行. 在一个程序中执行另一个执程序的方法有两种: 1)system 在shell中执行程序 2)fork + exec 复制一个进程,在进程中用新的程序替换原有的程序 for ...
- C#快速学习笔记(译)
下面是通过代码快速学习C#的例子. 1.学习任何语言都必定会学到的hello,world! using System; public class HelloWorld { public static ...
- webstorm ftp发布问题
通过webstorm发布遇到问题 Invalid descendent file name "/". 解决方案为 点击[Advanced options]勾选[always use ...
- html lang
目前,语言的标签表示法的国际标准是RFC 4646,名称是<Tags for Identifying Languages>.简单说,这个文件规定,一种语言的标签应该按照如下方式排列: la ...
- Postgresql 技巧
备份 pg_dump -f "F:/dump.sql"<file name> -U postgres<database name> -h 10.38.197 ...
- 闭包(Closures)
浅析 JavaScript 中的闭包(Closures) 一.前言 对于 JavaScript 来说,闭包是一个非常强大的特征.但对于刚开始接触的初学者来说它又似乎是特别高深的.今天我们一起来揭开闭包 ...
- 轻仿QQ音乐之音频歌词播放、锁屏歌词-b
先上效果图 歌词播放界面 音乐播放界面 锁屏歌词界面 一. 项目概述 前面内容实在是太基础..只想看知识点的同学可以直接跳到第三部分的干货 项目播放的mp3文件及lrc文件均来自QQ音乐 本文主要主要 ...
- ionicPopup确认对话框
$ionicPopup.confirm({ title: $rootScope.app_name, template: 'Do you want to add this to database?', ...