Description

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

实在想不出这算是什么网络流==

题意:会议室里面有n种插座,有m种(个)电器需要充电(即每种只有一个),有k种转换器(无限量,而且可以互相插)。问最少有多少电器没地方插

做法:电器在左边,插座类型在右边(这个题好像不仅有n种插座,电器和转换器都会出现新的插座类型)。电器的点连源点,流量1,;电器与它的插座类型连边,流量1;会议室的插座类型连汇点,流量1;转换器的类型连有向边,流量oo

#include <iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
const int mm=220005;
const int mn=22222;
const int oo=1000000000;
int node,src,dest,edge;
int reach[mm],flow[mm],nxt[mm];
int head[mn],work[mn],dis[mn],q[mn];
inline int min(int a,int b)
{
    return a<b?a:b;
}
inline void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    memset(head,-1,sizeof(head));
    edge=0;
}
inline void addedge(int u,int v,int c1)
{
    reach[edge]=v,flow[edge]=c1,nxt[edge]=head[u],head[u]=edge++;
    reach[edge]=u,flow[edge]=0,nxt[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0;i<node;++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0;l<r;++l)
        for(i=head[u=q[l]];i>=0;i=nxt[i])
            if(flow[i]&&dis[v=reach[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
int Dinic_dfs(int u,int exp)
{
    if(u==dest)return exp;
    for(int &i=work[u],v,tmp;i>=0;i=nxt[i])
        if(flow[i]&&dis[v=reach[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }dis[u]--;
    return 0;
}
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0;i<node;++i)work[i]=head[i];
        while(delta=Dinic_dfs(src,oo))ret+=delta;
    }
    return ret;
}
int  receptacles[200],plug[200][2],adapters[200][2];
int main()
{
   // freopen("cin.txt","r",stdin);
    int n,m,k,ind=0;
    map<string,int>M;
    string str1,str2;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>str1;
        if(!M[str1])M[str1]=++ind;
        receptacles[i]=M[str1];
    }
    cin>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>str1>>str2;
       // if(!M[str1])M[str1]=++ind;
        if(!M[str2])M[str2]=++ind;
        plug[i][0]=i;
        plug[i][1]=M[str2];
    }
    cin>>k;
    for(int i=1;i<=k;i++)
    {
        cin>>str1>>str2;
        if(!M[str1])M[str1]=++ind;
        if(!M[str2])M[str2]=++ind;
        adapters[i][0]=M[str1];
        adapters[i][1]=M[str2];
    }
    prepare(ind+2+m,0,m+ind+1);
    for(int i=1;i<=m;i++)
    {
        addedge(0,i,1);
        addedge(i,m+plug[i][1],1);
    }
    for(int i=1;i<=n;i++)
        addedge(receptacles[i]+m,dest,1);
    for(int i=1;i<=k;i++)
        addedge(adapters[i][0]+m,m+adapters[i][1],oo);
    cout<<m-Dinic_flow()<<endl;
    return 0;
}