A Plug for UNIX
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11636 | Accepted: 3834 |
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
Source
East Central North America 1999
#include<stdio.h> #include<map> #include<iostream> #include<string.h> #include<string> #include<queue> using namespace std; //**************************************************** //最大流模板 //初始化:g[][],start,end //****************************************************** const int MAXN=500; const int INF=0x3fffffff; int g[MAXN][MAXN];//存边的容量,没有边的初始化为0 int path[MAXN],flow[MAXN],start,end; int n;//点的个数,编号0-n.n包括了源点和汇点。 queue<int>q; int bfs() { int i,t; while(!q.empty())q.pop();//把清空队列 memset(path,-1,sizeof(path));//每次搜索前都把路径初始化成-1 path[start]=0; flow[start]=INF;//源点可以有无穷的流流进 q.push(start); while(!q.empty()) { t=q.front(); q.pop(); if(t==end)break; //枚举所有的点,如果点的编号起始点有变化可以改这里 for(i=0;i<=n;i++) { if(i!=start&&path[i]==-1&&g[t][i]) { flow[i]=flow[t]<g[t][i]?flow[t]:g[t][i]; q.push(i); path[i]=t; } } } if(path[end]==-1)return -1;//即找不到汇点上去了。找不到增广路径了 return flow[end]; } int Edmonds_Karp() { int max_flow=0; int step,now,pre; while((step=bfs())!=-1) { max_flow+=step; now=end; while(now!=start) { pre=path[now]; g[pre][now]-=step; g[now][pre]+=step; now=pre; } } return max_flow; } map<string,int>hash; int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); string str1,str2; int N,M; int tol; while(scanf("%d",&N)!=EOF) { hash.clear(); memset(g,0,sizeof(g)); start=0; end=1; tol=2; while(N--) { cin>>str1; hash[str1]=tol; g[0][tol]=1; tol++; } scanf("%d",&M); for(int i=0;i<M;i++) { cin>>str1>>str2; if(hash[str1]==0)hash[str1]=tol++; if(hash[str2]==0)hash[str2]=tol++; g[hash[str1]][end]=1; g[hash[str2]][hash[str1]]=1; } scanf("%d",&N); while(N--) { cin>>str1>>str2; if(hash[str1]==0)hash[str1]=tol++; if(hash[str2]==0)hash[str2]=tol++; g[hash[str2]][hash[str1]]=INF; } n=tol-1; printf("%d\n",M-Edmonds_Karp()); } return 0; }