A Plug for UNIX
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 14711 |
|
Accepted: 4959 |
Description
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 bureau Cratic 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 being long and tedious, you wan T to is able to plug in as many as 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 unlim ited supply of the ones they do has.
Input
The input would consist of one case. The first line contains a, positive integer n (1 <= n <=) indicating the number of receptacles in the . 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 <=) indicating the number of devices your would like T o plug in. 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 type O F receptacle it requires). A device name is a string of in most 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 <=) indicating the number of different varieties of a Dapters 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.
Output
A line containing a single non-negative integer indicating the smallest number of devices that cannot is plugged in.
Sample Input
Sample Output
1
n sockets, M electrical appliances and their corresponding sockets, K converters , the former one can be converted to a socket, ask at least how many devices do not have a socket, the number of converters unlimited
The maximum flow, the source point to the socket edge, the capacity of 1 , the appliance is built on the side of the meeting point and the capacity is 1 , the corresponding outlet and the electrical connection, the capacity is 1 , the former outlet is converted to the rear socket, and the rear one socket is built on the edge of an outlet, the capacity is infinite, The maximum flow that is obtained is the maximum number of paired appliances.
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < queue> #include <map> #define MAXN 10000#define maxm 1000000#define INF 0x3f3f3f3fusing namespace Std;int head[ MAXN], CUR[MAXN], Cnt;int DIST[MAXN], vis[maxn];int n;//socket number int m;//electrical number int k;//converter number int sect; Super sinks struct node{int u, V, cap, flow, next;}; Node edge[maxm];map<string, int>mp;void init () {cnt = 0; Memset (Head,-1, sizeof (head)); Mp.clear ();} void Add (int u, int v, int w) {edge[cnt] = {u, V, W, 0, Head[u]}; Head[u] = cnt++; EDGE[CNT] = {V, u, 0, 0, Head[v]}; HEAD[V] = cnt++;} void Getmap () {char s[30]; Char str[30]; int ans = 1; while (n--) {scanf ("%s", s); Mp[s] = ans++; Add (0, Mp[s], 1); 0 is the super Source point} scanf ("%d", &m); for (int i = 0; i < m; ++i) {scanf ("%s%s", str, s); MP[STR] = ans++; if (!mp[s]) {mp[s] = ans++; } Add (Mp[s], mp[str], 1); Add (Mp[str], sect, 1); } scanf ("%d", &k); while (k--) {scanf ("%s%s", S, str); if (!mp[s]) mp[s] = ans++; if (!mp[str]) mp[str] = ans++; Add (Mp[str], mp[s], INF);//rear one socket forward one socket build Edge}}bool BFS (int st, int ed) {queue<int>q; memset (Vis, 0, sizeof (VIS)); memset (Dist,-1, sizeof (Dist)); VIS[ST] = 1; DIST[ST] = 0; Q.push (ST); while (!q.empty ()) {int u = q.front (); Q.pop (); for (int i = head[u]; i =-1; i = Edge[i].next) {node E = Edge[i]; if (!VIS[E.V] && e.cap > E.flow) {vis[e.v] = 1; DIST[E.V] = Dist[u] + 1; if (e.v = = ed) return true; Q.push (E.V); }}} return false;} int DFS (int x, int ed, int a) {if (x = = Ed | | a = = 0) return A; int flow = 0, F; for (int &i = cur[x]; I! =-1; i = Edge[i].next) {node &e = Edge[i]; if (DIST[E.V]= = Dist[x] + 1 && (f = DFS (e.v, ed, Min (A, e.cap-e.flow))) > 0) {e.flow + = f; edge[i ^ 1].flow-= f; A-= f; Flow + + F; if (a = = 0) break; }} return flow;} int Maxflow (int st, int ed) {int flowsum = 0; while (BFS (St, ed)) {memcpy (cur, head, sizeof (head)); Flowsum + = DFS (St, Ed, INF); } return flowsum;} int main () {while (scanf ("%d", &n)!=eof) {sect = n + m + k + 1; Init (); Getmap (); int sum; sum = Maxflow (0, sect); printf ("%d\n", m-sum); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1087--a Plug for UNIX "Max Stream dinic"