POJ 1087 A Plug for UNIX (string numbering map)

Source: Internet
Author: User
Tags alphanumeric characters

A Plug for UNIX
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14862 Accepted: 5026

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

Source

East Central North America 1999

The title of the topic is too difficult to understand, but the topic of the meaning of clear after the better to do
In this topic there are two kinds of items, one is a socket, the other is an electrical outlet with only one Jack and a
A plug, electrical only one plug first there are n kinds of sockets, n kinds of sockets with a string representation, this n kind of plug
The seat can be understood to be plugged into the power outlet and then have m electrical appliances, now electrical to charge, electrical
The string indicates that each appliance has its own sockets (this socket can not be the N plug in the power
The outlet on the source, which can be another outlet) now has k information S1 S2 representative S1 socket can be plugged into S2
Plug up, it's like switching the plug a little bit. These S1 and S2 can also be not that n plug in the electricity
The outlet on the source gives these messages. How many appliances do you have that have no sockets to use?
Build diagram:
Build a source point, point to all appliances, capacity 1
All the appliances point to the plug they can plug in, the capacity is 1.
If a plug can be plugged into another plug, then the S1 points to the S2, the capacity is infinitely large
Point all plugs plugged into the power supply to the sink point with a capacity of 1


Then the maximum flow can be obtained from the source point to the sink point.
However, the map will be more complex, because it involves the processing of strings, so it is better to use the map container to do some

#include <stdio.h> #include <iostream> #include <string.h> #include <queue> #include <map > #include <algorithm>using namespace std; #define INF 0x3f3f3f3f#define M 1000struct node{int v,next,w;} Mp[m*m];int head[m],dis[m],cnt,st,et;void Add (int u,int v,int W) {mp[cnt].v=v;mp[cnt].w=w;mp[cnt].next=head[u];head[ u]=cnt++;mp[cnt].v=u;mp[cnt].w=0;//graph mp[cnt].next=head[v];head[v]=cnt++;} int BFs () {memset (dis,-1,sizeof (dis));queue<int> q;while (!q.empty ()) Q.pop ();d Is[st]=0;q.push (ST); Q.empty ()) {int U=q.front (); Q.pop (); for (int i=head[u];i!=-1;i=mp[i].next) {int v=mp[i].v;if (MP[I].W && dis[v] ==-1) {Dis[v]=dis[u]+1;q.push (v); if (V==et) return 1;}}} return 0;} int dinic (int u,int low) {if (U==et | | low==0) return low;int ans=low,i,a;for (i=head[u];i!=-1;i=mp[i].next) {int V=MP[I].V if (dis[v]==dis[u]+1 && mp[i].w && (A=dinic (V,min (ANS,MP[I].W)))) {mp[i].w-=a;mp[i^1].w+=a;// At the beginning of writing mp[i].v,, find a half-day error, wipe wipe ans-=a;if (ans==0) return low;} return Low-ans;}int main () {int T,i,j,k,n,tot;char str[30],ch[30];scanf ("%d", &n);map<string,int> m;cnt=0; tot=1;//to electrical appliance number memset (head,-1,sizeof (head)); M.clear (); st=0; et=m-1;//starting point, End for (i=0;i<n;i++) {scanf ("%s", str); M[str]=tot++;add (m[str],et,1);} scanf ("%d", &t), for (i=0;i<t;i++) {scanf ("%s%s", str,ch), M[str]=tot++;if (!m[ch]) M[ch]=tot++;add (st,m[str],1 ); Add (m[str],m[ch],1);} scanf ("%d", &k), for (i=0;i<k;i++) {scanf ("%s%s", str,ch), if (!m[str]) m[str]=tot++;if (!m[ch]) M[ch]=tot++;add ( M[str],m[ch],inf); }int Ans=0;while (BFS ()) {ans+=dinic (st,inf);}  printf ("%d\n", T-ans); return 0;}


POJ 1087 A Plug for UNIX (string numbering map)

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.