Original question:
Charge of setting up the Press hostel for the inaugural meeting of the All Nations Internet eXecutive (UNIX), which has a international mandate to make the free flow of information and ideas on the Internet as cumbersome and Burea Ucratic as possible. Since the designed to accommodate reporters and journalists from around the world, it's equipped with electrical Receptacles to suit the different shapes of plugs and voltages used by appliances in all of the countries that existed wh En the hostel was built. Unfortunately, the built many years ago when reporters used very few electric and electronic devices and is equip PED 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 was likely to Be-long and tedious, you want-be-able to-plug in as-many as you can. Before the meeting begins, you gather up all the devices so the reporters would like-to-use, and attempt-to-set them up. You notice this some of the devices use plugs for which there are no receptacle. You wonder if these devices is from countries this didn ' t exist when the the hostel was built. For some receptacles, there is several devices that use the corresponding plug. For other receptacles, there is
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 is allowed to being plugged into other adapters. The store does not has adapters for all possible combinations of plugs
and receptacles, but there was essentially an unlimited supply of the ones they does have.
Input
The input would 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 all 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. Each receptacle type consists of a string of in most alphanumeric characters. The next line contains a single positive integer m (1≤m≤100) indicating the number of devices do would like to plug I N. Each of the next m lines lists the name of a device followed by the type of plug it uses (which are identical to the Typ E of receptacle it requires). A device name is a string of alphanumeric characters. No.
Devices would 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 is available. Each of the next K lines describes a variety of adapter, giving the type of receptacle provided by the adapter, followed B Y 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 is 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
English:
There are n sockets, M units K converters. First give you n a socket, then give you M devices, contains two strings, respectively, the device name and device plug model. And then I'm going to give you K-converters, like the B-X in the sample, to convert the type B plug into an X-type plug, an infinite number of converters, and can be used in a string. Only one device can be connected to each outlet, and each device must have the same plug and socket. Now ask how many devices you can have left without being connected.
Code:
#include <bits/stdc++.h> using namespace std;
const int maxn=501;
const int inf=100000;
Map<string,int> Rec,mark;
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 A[MAXN];
int P[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;
while (true) {memset (a,0,sizeof (a));
Queue<int> Q;
Q.push (s);
A[s]=int_max; while (!
Q.empty ()) {int X=q.front (); Q.pop ();
for (int i=0;i<g[x].size (); i++) {edge& e = edges[g[x][i]];
if (!a[e.to]&&e.cap>e.flow) {p[e.to]=g[x][i];
A[e.to]=min (A[x],e.cap-e.flow);
Q.push (e.to);
}} if (A[t]) break;
} if (!a[t]) break;
for (int u=t;u!=s;u=edges[p[u]].from) {edges[p[u]].flow+=a[t];
EDGES[P[U]^1].FLOW-=A[T];
} Flow+=a[t];
} return flow;
}
};
Edmondskarp EK;
int main () {Ios::sync_with_stdio (false);
int n,m,t;
cin>>t;
while (t--) {rec.clear ();
Mark.clear ();
Ek.init (500);
cin>>n;
for (int i=1;i<=n;i++) {string S; cin>>s;
Rec[s]=i; EK.
Addedge (i,500,1);
} cin>>m;
for (int i=1;i<=m;i++) {string name,adp;
cin>>name>>adp;
mark[adp]++;
} int k;
cin>>k;
for (int i=1;i<=k;i++) {string f,t;
cin>>f>>t;
if (Rec.find (f) ==rec.end ()) {rec[f]=++n; EK.
Addedge (n,500,0);
} if (Rec.find (t) ==rec.end ()) {rec[t]=++n; EK.
Addedge (n,500,0); } EK.
Addedge (Rec[f],rec[t],inf);
} for (auto It=mark.begin (); It!=mark.end (); it++) {if (Rec.find (It->first) ==rec.end ())
Continue Else EK.
Addedge (0,rec[it->first],it->second); } int Ans=ek.
Maxflow (0,500); Cout<<m-ans<<endl;
if (t) cout<<endl;
} return 0;
}
idea:
The exercises in the graph theory are the most obvious matching problems, but there are some ways to do it in the drawing.
According to the pattern of the maximum matching of the binary graph, first add a source point S and a meeting point T. Connect all the given n sockets to the sink point, because a device can only connect a socket, so the device to the sink point weight of 1, such as the black Edge and node. Because the device and socket to the same model, so the source point S is connected to the corresponding socket, if there are multiple sockets of the same equipment, then the number of the device is S-to-socket weights, such as the red side of the figure. Finally, the converter on the device to establish an edge, such as a B-X converter on the B-to-X to establish an infinite weight, if the socket in the converter is not in the given n socket, then also to add a new node, but to the meeting point of the weight to be set to 0, as shown in the blue side of the figure. The
finally seek the maximum flow from the starting point to the end point, because the number of edges and the number of new nodes in the converter is unknown, so the number of nodes should be set at about 500.