A Plug for UNIX
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 several case. The first line of the input contains the number of cases, and it’s followed bya blank line. The first line of each case 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.
There’s a blank line between test cases.
Output
For each case, print a line containing a single non-negative integer indicating the smallest number of devices that cannot be plugged in. Print a blank line between cases.
Sample Input
1
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
Miguel A. Revilla
2000-02-09
我有话说:
题目大意可参见刘汝佳紫书。
这道题可以说是最大流的裸算法。模板打一下就完事。最重要的思路是构图和如何应用网络流这一模型。我们很容易想到转换器是一条弧,那么设备插头就是一个点了。然后设源点s,汇点t,从s到设备devcie拉一条容量为1的边,从插板target拉一条容量为1的边到汇点。然后求最大流即可。中间我们还可以用flyod处理插头i能不能转换成插头j,如果可以那么也拉一条边,容量为无限大
#include<iostream>
#include<string>
#include<cstring>
#include<sstream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstdio>
using namespace std;
const int maxn=400+10;
const int INF=100000000;
struct Edge{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct EdmondsKarp{
int n,m;
vector<Edge>edges;
vector<int>G[maxn];
int p[maxn];
int a[maxn];
void init(int n){
for(int i=0;i<n;i++)G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap){
edges.push_back(Edge(from,to,cap,0));
edges.push_back(Edge(to,from,0,0));
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
int Maxflow(int s,int t){
int flow=0;
for(;;){
queue<int>Q;
memset(a,0,sizeof(a));
Q.push(s);a[s]=INF;
while(!Q.empty()){
int u=Q.front();Q.pop();
for(int i=0;i<G[u].size();i++){
Edge& e=edges[G[u][i]];
if(!a[e.to]&&e.cap>e.flow){
a[e.to]=min(a[u],e.cap-e.flow);
p[e.to]=G[u][i];
Q.push(e.to);
}
}
if(a[t])break;
}
if(!a[t])break;
flow+=a[t];
for(int u=t;u!=s;u=edges[p[u]].from){
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
}
}
return flow;
}
};
EdmondsKarp solver;
vector<string>names;
int ID(const string& s){
for(int i=0;i<names.size();i++){
if(s==names[i])return i;
}
names.push_back(s);
return names.size()-1;
}
int n,m,k;
int device[maxn];
int target[maxn];
int d[maxn][maxn];
int main()
{
int T;
cin>>T;
while(T--){
names.clear();
string s1,s2;
cin>>n;
for(int i=0;i<n;i++){
cin>>s1;
target[i]=ID(s1);//插座i对应的插头
}
cin>>m;
for(int i=0;i<m;i++){
cin>>s1>>s2;
device[i]=ID(s2);//设备i的插头类型
}
memset(d,0,sizeof(d));
cin>>k;
for(int i=0;i<k;i++){
cin>>s1>>s2;
d[ID(s1)][ID(s2)]=1;//插头s1可以转换成插头s2
}
int V=names.size();
for(int k=0;k<V;k++)
for(int i=0;i<V;i++)
for(int j=0;j<V;j++)
d[i][j]|=d[i][k]&&d[k][j];
solver.init(V+2);
for(int i=0;i<m;i++)
solver.AddEdge(V,device[i],1);
for(int i=0;i<n;i++)
solver.AddEdge(target[i],V+1,1);
for(int i=0;i<V;i++)
for(int j=0;j<V;j++)
if(d[i][j])solver.AddEdge(i,j,INF);
int r=solver.Maxflow(V,V+1);
cout<<m-r<<endl;
if(T)cout<<endl;
}
return 0;
}