最大流。

流可以对应一种分配方式。

显然最大流就可以表示最多匹配数

#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的更多相关文章

  1. 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 ...

  2. UVa753/POJ1087_A Plug for UNIX(网络流最大流)(小白书图论专题)

    解题报告 题意: n个插头m个设备k种转换器.求有多少设备无法插入. 思路: 定义源点和汇点,源点和设备相连,容量为1. 汇点和插头相连,容量也为1. 插头和设备相连,容量也为1. 可转换插头相连,容 ...

  3. 【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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. POJ1087 A Plug for UNIX(网络流)

                                       A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 关于javascript的window.onscroll方法

    ---恢复内容开始--- 今天在学习javascript的过程中被onscroll这个东西堵了一下午.心情极度郁闷. 在高度较大的网页中,我们通常会加一个返回顶部的按钮,方便用户操作. 代码如下: & ...

  2. Java 多线程 简单实例 (Runnable)

    1.多线程实例 package second; public class A implements Runnable { public char stat = '*'; public void run ...

  3. [译] ASP.NET 生命周期 – ASP.NET 请求生命周期(二)

    ASP.NET 请求生命周期 全局应用类也可以用来跟踪每个独立请求的生命周期,包括请求从 ASP.NET 平台传递到 MVC 框架.ASP.NET 框架会创建一个定义在 Global.asax 文件中 ...

  4. 读取app.config配置文件信息

    <?xml version="1.0" encoding="utf-8" ?> <configuration> <startup& ...

  5. DNF技能贴图的研究

    一直在猜想DNF的技能贴图怎么贴的,靠在游戏里慢慢移动确定技能的偏移太费时间了.前段发现了“可视坐标生成”这软件,针对DNF改衣服,装备款式的小工具,就自己写了个类似的. 从图上看,技能的域中心点和人 ...

  6. 类似nike+、香蕉打卡的转场动画效果-b

    首先,支持并感谢@wazrx 的 http://www.jianshu.com/p/45434f73019e和@onevcat 的https://onevcat.com/2013/10/vc-tran ...

  7. ANN中Precision-Recall权衡

    如果想要得到较高的精度,则需要较长的编码. 编码长度m增长的话,则item碰撞的概率会成倍的减小,从而导致召回率下降. 为了得到较高的召回率,则需要多个哈希表. 参考http://yongyuan.n ...

  8. [转载]mvc使用JsonResult返回Json数据

    controller 中定义以下方法: public JsonResult UpdateSingle(int id, string actionName, string actionValue) { ...

  9. Competing in a data science contest without reading the data

    Competing in a data science contest without reading the data Machine learning competitions have beco ...

  10. 在自己的网站上实现QQ授权登录

    最近在实现QQ授权登录,现将我的实现过程以及我的理解整理如下.以下所述如有不对之处,请指正. 官方提供的SDK有:JS,PHP,Java.我的网站使用Scala+Play搭建的,所以只能用JS SDk ...