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 by a 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
题意:有n个插座,m个设备和k种转换器,每种转换器有无限多个。已知每种插座设备和插头的类型。要求插的设备尽量多,问最少剩几个不匹配的设备。
分析:电器从1到m标号,转换器从m+1到m+k标号,插座从m+k+1到m+k+n标号,另外设置一个源点s=0,汇点t=m+k+n+1 , 求s到t的最大流。s与所有电器建一条有向边,容量为1,所有插座与汇点建一条有向边,容量为1。对于每个电器,如果能直接和插座相连的,和每个能相连的插座建一条有向边,容量为1.另外,所有电器和所有能连接上的转换器建一条有向边,容量为INF,转换器和转换器之间能相连的建一条有向边容量为inf,转换器和插座能相连建一条有向边容量为inf。用EK算法模板求出最大流量,最后答案就是m-最大流量。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
#define LL long long
#define Maxn 505
int n,m,k,tn,tm,tk,cap[Maxn][Maxn];
char rec[Maxn][51],di[Maxn][51],numr[Maxn];
inline void init() {
tn = tm = tk = 0;
memset(numr,0,sizeof(numr));
memset(cap,0,sizeof(cap));
}
inline int Num(char str[]) {
for(int i=0; i<tn; i++) {
if(strcmp(rec[i],str) == 0) return i;
}
return -1;
}
int EK(int S,int T) {
int f,flow[Maxn][Maxn],a[Maxn],p[Maxn];
queue<int> q;
memset(flow,0,sizeof(flow));
f = 0;
while(1) {
memset(a,0,sizeof(a));
a[S] = INF;
q.push(S);
while(!q.empty()) {
int u = q.front(); q.pop();
for(int v=0; v<n; v++) {
if(!a[v] && cap[u][v] > flow[u][v] ) {
p[v] = u;
q.push(v);
a[v] = min(a[u],cap[u][v] - flow[u][v]);
}
}
}
if(a[T] == 0) break;
for(int u=T; u!=S; u=p[u]) {
flow[p[u]][u] += a[T];
flow[u][p[u]] -= a[T];
}
f += a[T];
}
return f;
}
int main() {
int T; char str[51],strr[51];
scanf("%d",&T);
while(T--) {
init();
scanf("%d",&n);
for(int i=1; i<=n; i++) {
scanf("%s",str);
int pos = Num(str);
if(pos == -1) {;
strcpy(rec[tn],str);
numr[tn++] = 1;
}
else numr[pos]++;
}
scanf("%d",&m);
for(int i=1; i<=m; i++) {
scanf("%s%s",di[i],str);
int pos = Num(str);
if(pos == -1) {
pos = tn;
strcpy(rec[tn], str);
numr[tn++] = 0;
}
cap[0][i] = 1;
cap[i][m + 1 + pos] = 1;
}
scanf("%d",&k);
for(int i=1; i<=k; i++) {
scanf("%s%s",str,strr);
if(Num(str) == -1) {
strcpy(rec[tn],str);
numr[tn++] = 0;
}
if(Num(strr) == -1) {
strcpy(rec[tn],strr);
numr[tn++] = 0;
}
cap[m + 1 + Num(str)][m + 1 + Num(strr)] = INF;
}
for(int i=0; i<tn; i++)
cap[m + i + 1][m + tn + 1] = numr[i];
n = m + tn + 2;
int Ans = EK(0,n - 1);
cout << m - Ans << endl;
if(T) cout << endl;
}
return 0;
}