You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an international mandate to make the free flow of information and ideas on the Internet as cumbersome and bureaucratic as possible. 
Since the room was designed to accommodate reporters and journalists from around the world, it is equipped with electrical receptacles to suit the different shapes of plugs and voltages used by appliances in all of the countries that existed when the room was built. Unfortunately, the room was built many years ago when reporters used very few electric and electronic devices and is equipped with only one receptacle of each type. These days, like everyone else, reporters require many such devices to do their jobs: laptops, cell phones, tape recorders, pagers, coffee pots, microwave ovens, blow dryers, curling 
irons, tooth brushes, etc. Naturally, many of these devices can operate on batteries, but since the meeting is likely to be long and tedious, you want to be able to plug in as many as you can. 
Before the meeting begins, you gather up all the devices that the reporters would like to use, and attempt to set them up. You notice that some of the devices use plugs for which there is no receptacle. You wonder if these devices are from countries that didn't exist when the room was built. For some receptacles, there are several devices that use the corresponding plug. For other receptacles, there are no devices that use the corresponding plug. 
In order to try to solve the problem you visit a nearby parts supply store. The store sells adapters that allow one type of plug to be used in a different type of outlet. Moreover, adapters are allowed to be plugged into other adapters. The store does not have adapters for all possible combinations of plugs and receptacles, but there is essentially an unlimited supply of the ones they do have.

Input

The input will consist of one case. The first line contains a single positive integer n (1 <= n <= 100) indicating the number of receptacles in the room. The next n lines list the receptacle types found in the room. Each receptacle type consists of a string of at most 24 alphanumeric characters. The next line contains a single positive integer m (1 <= m <= 100) indicating the number of devices you would like to plug in. Each of the next m lines lists the name of a device followed by the type of plug it uses (which is identical to the type of receptacle it requires). A device name is a string of at most 24 alphanumeric 
characters. No two devices will have exactly the same name. The plug type is separated from the device name by a space. The next line contains a single positive integer k (1 <= k <= 100) indicating the number of different varieties of adapters that are available. Each of the next k lines describes a variety of adapter, giving the type of receptacle provided by the adapter, followed by a space, followed by the type of plug.

Output

A line containing a single non-negative integer indicating the smallest number of devices that cannot be plugged in.

Sample Input

4
A
B
C
D
5
laptop B
phone C
pager B
clock B
comb X
3
B X
X A
X D

Sample Output

1
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=;
const int maxm=;
const int inf=1e9+;
int n,m,k;
int s,t;
int head[maxn];
int Next[maxm];
int depth[maxn];
int cnt;
struct edge{
int u,v,w;
}e[maxm];
struct cz{
char s[];
}c[maxn];
struct dq{
char s1[],s2[];
}d[maxn];
struct zhq{
char s1[],s2[];
}z[maxn];
void addedge(int u,int v,int w)
{
cnt++;
Next[cnt]=head[u];
head[u]=cnt;
e[cnt].u=u;
e[cnt].v=v;
e[cnt].w=w;
cnt++;
Next[cnt]=head[v];
head[v]=cnt;
e[cnt].u=v;
e[cnt].v=u;
e[cnt].w=;
}
int bfs()
{
queue<int>q;
memset(depth,-,sizeof(depth));
depth[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=Next[i])
{
int v=e[i].v;
if((depth[v]==-)&&(e[i].w>))
{
depth[v]=depth[u]+;
q.push(v);
}
}
}
if(depth[t]==-)
return ;
return ;
}
int dfs(int u,int w)
{
if(u==t)
return w;
for(int i=head[u];i!=-;i=Next[i])
{
int v=e[i].v;
if((depth[v]==depth[u]+)&&(e[i].w>))
{
int di=dfs(v,min(w,e[i].w));
if(di>)
{
e[i].w-=di;
e[i^].w+=di;
return di;
}
}
}
return ;
}
int main()
{
while(~scanf("%d",&n))
{
memset(head,-,sizeof(head));
cnt=-;
int i;
s=;
for(i=;i<=n;i++)
{
scanf("%s",c[i].s);
}
scanf("%d",&m);
for(i=;i<=m;i++)
{
scanf("%s%s",d[i].s1,d[i].s2);
}
scanf("%d",&k);
for(i=;i<=k;i++)
{
scanf("%s%s",z[i].s1,z[i].s2);
}
t=n+m+k+;
for(i=;i<=m;i++)
{
addedge(s,i,);
for(int j=;j<=k;j++)
{
if(!strcmp(d[i].s2,z[j].s1))
addedge(i,j+m,inf);
}
for(int j=;j<=n;j++)
{
if(!strcmp(d[i].s2,c[j].s))
addedge(i,j+m+k,inf);
}
}
for(i=;i<=k;i++)
{
for(int j=;j<=k;j++)
{
if(i!=j&&!strcmp(z[i].s2,z[j].s1))
addedge(i+m,j+m,inf);
}
for(int j=;j<=n;j++)
{
if(!strcmp(z[i].s2,c[j].s))
addedge(i+m,m+k+j,inf);
}
}
for(i=;i<=n;i++)
{
addedge(m+k+i,t,);
}
int ans=;
while(bfs())
{
while(int di=dfs(,inf))
ans+=di;
}
printf("%d\n",m-ans);
}
return ;
}

网络流的算法,EK的比较简单,这是dinic的算法,其中有两个数组,不容易看懂,一个是head数组,一个是next数组。

next这个名字起的实际上也对,因为它是循环时候的那个  下一个   的意思,但是里面存入的是这个边上一个边的编号。

这样for循环的时候,bfs()for循环里面有判断条件,直接跳转到源,然后开始进去队列。

dfs() for 循环的时候,本身dfs就是回溯的一个算法,一直往回找,这样正顺应着dfs的思路,一直去寻找上一条边。

dinic网络流的更多相关文章

  1. DINIC网络流+当前弧优化

    DINIC网络流+当前弧优化 const inf=; type rec=record s,e,w,next:longint; end; var b,bb,d,q,tb:..] of longint; ...

  2. [codevs1227]草地排水<Dinic网络流最大流>

    题目链接:http://codevs.cn/problem/1993/ https://www.luogu.org/problemnew/show/P2740 之前一直都没去管网络流这算法,但是老师最 ...

  3. Dinic 网络流

    写个博客贴板子-- inline void add_edge(int x,int y,int z){ e[++tot].x=y,e[tot].cap=z; e[tot].next=h[x],h[x]= ...

  4. dinic网络流模板

    src:源点 sink:汇点 #include<queue> #include<iostream> #include<string.h> #include<s ...

  5. Internship-ZOJ2532(网络流求割边)

    Internship Time Limit: 5 Seconds      Memory Limit: 32768 KB CIA headquarter collects data from acro ...

  6. HDU 3416 Marriage Match IV dij+dinic

    题意:给你n个点,m条边的图(有向图,记住一定是有向图),给定起点和终点,问你从起点到终点有几条不同的最短路 分析:不同的最短路,即一条边也不能相同,然后刚开始我的想法是找到一条删一条,然后光荣TLE ...

  7. ZOJ 2532 网络流最小割

    求最小割的问题. 题意:已知网络中有n个源点,m的中转站(也就是节点),一个汇点(编号为0).给出网络,求一些边(增大这个边就可以增大汇点流量的边). 思路:一开始代码只找了有流=0就加入输出数组的情 ...

  8. POJ2987 Firing 最大权闭合图

    详情请参考http://www.cnblogs.com/kane0526/archive/2013/04/05/3001557.html 值得注意的地方,割边会把图分成两部分,一部分和起点相连,另一部 ...

  9. 【HDOJ】3505 Writing Robot

    挺好的一道题目,我的做法是kmp+Dinic网络流.kmp求子串在P中出现的次数,从而计算love值.网络流主要用来处理最优解.case2中p1的love值是8,p2的love值是7,最终T包含p1和 ...

随机推荐

  1. 2016 Multi-University Training Contest 1Abandoned country

    qaq,现在内心真是各种草泥马.怪自己见识短浅...哎... 题意: 给你一幅图,然后求一个最小花费使得所有的点都连通(这就是最小生成树啊),然后在这棵树上[如果我要从任意起点到任意终点,这两个点不同 ...

  2. hdoj1078【DP·记忆化搜索】

    还是满水的一道题目吧...这个一看肯定要搜索了..然后又是这么DP,那就是记忆化搜索了...走K步,下一步要比他多...很好写啊/// #include<iostream> #includ ...

  3. 洛谷P3825 [NOI2017]游戏(2-SAT)

    传送门 果然图论的题永远建图最麻烦……看着题解代码的建图过程真的很珂怕…… 先不考虑地图$x$,那么每一个地图都只能用两种赛车,于是我们可以用2-SAT来搞,用$i$表示这个地图能用的第一辆车,$i' ...

  4. EasyUI创建选项卡并判断是否打开

    //创建选项卡:判断选项卡是否打开,如果以打开则定位到选项卡,否则创建 function addPanel(title) { var bol = $('#main_tabs').tabs('exist ...

  5. update cdh version ,but cdh use old conf ,problem solve

    最近升级cdh版本,从4.5 升级到 5.0.0 beta-2 但是升级后,发现/etc/alternatives 路径下的软链接还是只想旧的4.5 版本,而且hadoop环境也是沿用4.5 的版本c ...

  6. apcloud混合式开发app学习笔记

    修改图标新建项目检出到本地--------------------------1.api.ajax var loginName = $api.val($api.byId('uname')); var ...

  7. PHP + jquery.validate remote的用法

    [ 转 ] http://www.cnlvzi.com/index.php/Index/article/id/58 最近做验证功能时,用到jquery.validate.js中的remote远程验证方 ...

  8. 哈密顿图 BestCoder Round #53 (div.2) 1003 Rikka with Graph II

    题目传送门 题意:判断是否为哈密顿图 分析:首先一种情况是不合法的:也就是度数为1的点超过2个:合法的有:,那么从度数为1的点开始深搜,如果存在一种走法能够走完n个点那么存在哈密顿路 收获:学习资料 ...

  9. dubbo中Hessian方法重载问题处理

    dubbo中Hessian方法重载,报出如下错误信息: 十一月 , :: 下午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Ser ...

  10. PV,UV,IP概念

    PV是网站分析的一个术语,用以衡量网站用户访问的网页的数量.对于广告主,PV值可预期它可以带来多少广告收入.一般来说,PV与来访者的数量成正比,但是PV并不直接决定页面的真实来访者数量,如同一个来访者 ...