# ZOJ-1157 A Plug for UNIX 【最大流】

**Problem**

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.

**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**

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

**题解：**

最大流建模，难度不大。设源点到每个原始插座的边容量为1，然后再统计对每种插座的需求量，然后把每种转换插座之间连边，容量为正无穷，因为可以提供无限个，最后把需求的插座和汇点连边，容量为需求量。

**代码：**

1 #include<bits/stdc++.h> 2 using namespace std; 3 #define INF 0x3f3f3f3f 4 #define M(a, b) memset(a, b, sizeof(a)) 5 const int N = 500 + 5; 6 int tot, num[N]; 7 struct Edge { 8 int from, to, cap, flow; 9 }; 10 11 struct Dinic { 12 int n, m, s, t; 13 vector<Edge> edges; 14 vector<int> G[N]; 15 bool vis[N]; 16 int d[N], cur[N]; 17 18 void init(int n) { 19 for (int i = 0; i <= n+1; ++i) G[i].clear(); 20 edges.clear(); M(d, 0); 21 tot = 1; M(num, 0); 22 } 23 24 void AddEdge(int from, int to, int cap) { 25 edges.push_back((Edge){from, to, cap, 0}); 26 edges.push_back((Edge){to, from, 0, 0}); 27 m = edges.size(); 28 G[from].push_back(m-2); G[to].push_back(m-1); 29 } 30 31 bool bfs() { 32 M(vis, 0); 33 queue<int> q; 34 q.push(s); 35 d[s] = 0; vis[s] = 1; 36 while (!q.empty()) { 37 int x = q.front(); q.pop(); 38 for (int i = 0; i < G[x].size(); ++i) { 39 Edge &e = edges[G[x][i]]; 40 if (!vis[e.to] && e.cap > e.flow) { 41 vis[e.to] = 1; 42 d[e.to] = d[x] + 1; 43 q.push(e.to); 44 } 45 } 46 } 47 return vis[t]; 48 } 49 50 int dfs(int x, int a) { 51 if (x == t || a == 0) return a; 52 int flow = 0, f; 53 for (int &i = cur[x]; i < G[x].size(); ++i) { 54 Edge &e = edges[G[x][i]]; 55 if (d[e.to] == d[x] + 1 && (f = dfs(e.to, min(a, e.cap-e.flow))) > 0) { 56 e.flow += f; 57 edges[G[x][i]^1].flow -= f; 58 flow += f; a -= f; 59 if (a == 0) break; 60 } 61 } 62 return flow; 63 } 64 65 int Maxflow(int s, int t) { 66 this->s = s; this->t = t; 67 int flow = 0; 68 while (bfs()) { 69 M(cur, 0); 70 flow += dfs(s, INF); 71 } 72 return flow; 73 } 74 75 }solver; 76 77 map<string, int> mp; 78 int get_id(string s) { 79 if (!mp[s]) mp[s] = ++tot; 80 return mp[s]; 81 } 82 83 int main() { 84 ios::sync_with_stdio(false); 85 int T, n, m, k; 86 string s, s1; 87 cin >> T; 88 while (T--) { 89 cin >> n; 90 solver.init(n); 91 mp.clear(); 92 for (int i = 0; i < n; ++i) { 93 cin >> s; 94 solver.AddEdge(0, get_id(s), 1); 95 } 96 cin >> m; 97 for (int i = 0; i < m; ++i) { 98 cin >> s1 >> s; 99 ++num[get_id(s)]; 100 } 101 cin >> k; 102 for (int i = 0; i < k; ++i) { 103 cin >> s >> s1; 104 solver.AddEdge(get_id(s1), get_id(s), INF); 105 } 106 for (int i = 2; i <= tot; ++i) 107 if (num[i]) solver.AddEdge(i, 1, num[i]); 108 cout << m-solver.Maxflow(0, 1) << endl; 109 if (T) cout << endl; 110 } 111 112 return 0; 113 }