A Plug for UNIX
http://acm.zju.edu.cn/show_problem.php?pid=1157
Time limit: 10 Seconds
Memory limit: 32768K
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.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
1. 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.
2. Output
A line containing a single non-negative integer indicating the smallest number of devices that cannot be plugged in.
3. 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
4. Sample Output
1
Problem Source: East Central North America 1999
1 // written by 曹高挺
2 // 2007-02-26 20:06:46 Accepted 1157 C++ 00:00.02 1060K
3
4 // 二分匹配
5 // 设备匹配插孔,每种类型的原插孔只有一个,适配器同一类型有无穷多个
6
7 #include <iostream>
8 #include <vector>
9 #include <string>
10 #include <map>
11
12 using namespace std;
13
14 int nRecep, nDev, nAdpt; // 原插孔类型数,设备数,适配器类型数
15 // 这三个数都是<=100的,但是在最坏情况下插针数有400种
16
17 int m1to2[400];
18 int m2to1[400];
19 bool conn[400][400];
20 bool visited[400];
21
22 map<string, int> recep; // 插孔名称和其对应的ID
23 map<string, int>::iterator miter;
24 int nhi;
25
26 int devtype[400]; // 各设备插针类型
27 vector<int> adpt[400]; // 记录所有可以直接转换的插针,计算完毕后包括可以间接转换的插针
28
29 int qu[400]; // BFS时用
30 int qlen;
31
32 int IndexOf(string& str){
33 if( (miter=recep.find(str))==recep.end() ){
34 recep[str] = nhi++;
35 return nhi-1;
36 }
37 return miter->second;
38 }
39
40 void Init(){
41 memset(conn, false, sizeof(conn));
42 memset(m1to2, -1, sizeof(m1to2));
43 memset(m2to1, -1, sizeof(m2to1));
44 recep.clear();
45 nhi = 0;
46 for(int i=0; i<300; i++){
47 adpt[i].resize(0);
48 }
49 }
50
51 void GetInfo(){
52 int i;
53 string name;
54 string name2;
55
56 // 插孔加入ID映射表
57 cin>>nRecep;
58 for(i=0; i<nRecep; i++){
59 cin>>name;
60 IndexOf(name);
61 }
62
63 // 计算各设备的插针类型
64 cin>>nDev;
65 for(i=0; i<nDev; i++){
66 cin>>name>>name;
67 devtype[i] = IndexOf(name);
68 }
69
70 // 获取适配器信息
71 cin>>nAdpt;
72 for(i=0; i<nAdpt; i++){
73 cin>>name>>name2;
74 adpt[ IndexOf(name) ].push_back( IndexOf(name2) );
75 }
76
77 // 此时nhi为插针类型数 (nhi>=nRecep)
78 }
79
80 void BFS(int from){
81 // 广度优先遍历,计算from类型针脚所有可以转换的针脚
82 qlen = 0;
83 memset(visited, false, sizeof(bool)*nhi);
84 visited[from] = true;
85 int i, iter=0;
86 int tp, nexttp;
87
88 for(i=0; i<(int)adpt[from].size(); i++){
89 qu[qlen++] = adpt[from][i];
90 visited[ adpt[from][i] ] = true;
91 }
92 while( iter<qlen ){
93 tp = qu[iter]; // 当前插针类型
94 for(i=0; i<(int)adpt[tp].size(); i++){
95 nexttp = adpt[tp][i]; // 转换后的指针类型
96 if( !visited[ nexttp ] ){
97 visited[ nexttp ] = true;
98 adpt[from].push_back( nexttp );
99 qu[qlen++] = nexttp;
100 }
101 }
102 iter ++;
103 }
104 }
105
106 void Plug(){
107 // 计算出所有可以转换的插针,计算结果在adpt里
108 int i, j;
109 int tp;
110 for(i=0; i<nhi; i++){
111 BFS(i);
112 }
113
114 // 构二分图
115 for(i=0; i<nDev; i++){
116 tp = devtype[i];
117 conn[i][tp] = true;
118 for(j=0; j<(int)adpt[tp].size(); j++){
119 if( adpt[tp][j] < nRecep ){
120 conn[i][adpt[tp][j]] = true;
121 }
122 }
123 }
124 }
125
126 bool FindPath(int nn){
127 int i;
128 for(i=0; i<nRecep; i++){
129 if( !visited[i] && conn[nn][i] ){
130 visited[i] = true;
131 if( m2to1[i]==-1 || FindPath(m2to1[i]) ){
132 m1to2[nn] = i;
133 m2to1[i] = nn;
134 return true;
135 }
136 }
137 }
138 return false;
139 }
140
141 int Match(){
142 int ret = 0;
143 int i;
144 for(i=0; i<nDev; i++){
145 if( m1to2[i]==-1 ){
146 memset(visited, false, sizeof(bool)*nRecep);
147 ret += FindPath(i);
148 }
149 }
150 return ret;
151 }
152
153 int main(){
154 int T, k;
155 cin>>T;
156 for(k=0; k<T; k++){
157 if( k ) printf("\n");
158 Init();
159 GetInfo();
160 Plug();
161 printf("%d\n", nDev-Match());
162 }
163
164 return 1;
165 }