Poj1087 a plug for Unix

Source: Internet
Author: User
A plug for Unix
Time limit:1000 ms Memory limit:65536 K
Total submissions:12438 Accepted:4136

Description

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 appliances tacles 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 policyears ago when reporters used very few electric and electronic devices and is equipped with only one primary tacle of each type. these days, like everyone else, reporters require restart such devices to do their jobs:
Laptops, cell phones, tape recorders, pagers, coffee pots, microwave ovens, blow dryers, curling
Irons, tooth brushes, etc. naturally, thanks 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 ready as you can.
Before the meeting begins, you gather up all the devices that the reporters wocould like to use, and attempt to set them up. you notice that some of the devices use plugs for which there is no such tacle. you wonder if these devices are from countries that didn't
Exist when the room was built. For some seconds tacles, there are several devices that use the corresponding plug. For other login tacles, 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 essential an unlimited supply of the ones they do have.

Input

The input will consist of one case. the first line contains a single positive integer N (1 <= n <= 100) indicating the number of specified tacles in the room. the next n lines list the specified tacle types found
In the room. each role tacle 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 wowould like to plug in. each of the next M lines lists
Name of a device followed by the type of plug it uses (which is identical to the type of each tacle 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 role tacle 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

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
 
Idea: introduce Super Source Vertex S and super sink vertex T. For the socket I provided by all the conference rooms, create an edge S-> I, and the edge capacity is 1 (because each socket only provides one ); for each device's socket I, create an edge I-> T (note that different devices may have the same socket type, so the edge capacity is equal to the number of devices with the same socket type ); for each type of converter (I, j), create an edge I-> J and the edge capacity is Inf (because the number of converters of the same type is unlimited ).
PS: the device and converter may have a socket type that is not available in the meeting room, so make sure that no node is left behind when creating the diagram.
# Include <iostream> # include <cstring> # include <cstdio> using namespace STD; const int maxn = 405; const int INF = (1 <29 ); int flow [maxn] [maxn]; int level [maxn]; int n, m, S, T; void build_graph () {char type [maxn] [30], device [30], a [30], B [30], tempn; int I, j, k; tempn = N; for (I = 1; I <= N; I ++) scanf ("% s", type [I]); memset (flow, 0, sizeof (flow); scanf ("% d", & M ); T = 0; // sink point for (I = 1; I <= m; I ++) {scanf ("% * S % s", device ); for (j = 1; j <= N; J ++) if (strcmp (device, type [J]) = 0) {flow [J] [T] ++; // note that multiple devices have the same socket type break;} If (j> N) // no socket type {n ++; strcpy (type [N], device); flow [N] [T] = 1 ;}} scanf ("% d", & K); While (k --) {scanf ("% S % s", a, B); for (I = 1; I <= N; I ++) if (strcmp (, type [I]) = 0) break; if (I> N) {n ++; strcpy (type [N], a) ;}for (j = 1; j <= N; j ++) if (strcmp (B, type [J]) = 0) break; If (j> N) {n ++; strcpy (type [N], B);} flow [J] [I] = inf; // unlimited number of converters of the same type} n ++; S = N; // source point for (I = 1; I <= tempn; I ++) flow [s] [I] = 1;} int BFS () {int queue [maxn], front, rear; memset (Level, 0, sizeof (level); front = rear = 0; level [s] = 1; queue [rear ++] = s; while (front! = Rear) {int v = queue [Front ++]; for (INT I = 0; I <= N; I ++) if (! Level [I] & flow [v] [I]) {level [I] = level [v] + 1; queue [rear ++] = I ;}} return Level [T];} int DFS (int I, int f) {if (I = T) return F; int sum = 0; For (Int J = 0; F & J <= N; j ++) {If (level [J] = level [I] + 1 & flow [I] [J]) {int TMP = DFS (J, min (F, flow [I] [J]); F-= TMP; flow [I] [J]-= TMP; flow [J] [I] + = TMP; sum + = TMP;} return sum;} int dinic () {int maxflow = 0; while (BFS ()) maxflow + = DFS (S, INF); Return maxflow;} int main () {While (~ Scanf ("% d", & N) {build_graph (); printf ("% d \ n", M-dinic () ;}return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.