POJ 1087 Max Stream bare topic + map

Source: Internet
Author: User
Tags alphanumeric characters

A Plug for UNIX
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15597 Accepted: 5308

Description

Charge of setting up the Press hostel for the inaugural meeting of the The same Nations Internet eXecutive (UNI X), which has a international mandate to make the free flow of information and ideas on the Internet as cumbersome and bu Reaucratic as possible.
Since the designed to accommodate reporters and journalists from around the world, it's equipped with ELECTR ical receptacles to suit the different shapes of plugs and voltages used by appliances in all of the countries that Existe D when 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, the reporters would as-is, 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 this cannot be plugged in.

Sample Input

Sample Output

1
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath > #include <vector> #include <queue> #include <map> #include <algorithm> #include <set>  using namespace std; #define MM (A, B) memset (A,b,sizeof (a)) typedef long Long ll;typedef unsigned long long ull;const int mod = 1000000007;const double EPS = 1e-10;const int inf = 0x3f3f3f3f;const int big=50000;int max (int a,int b) {return a>b? A:B;}; int min (int a,int b) {return a<b?a:b;}; struct edge{int to,cap,rev;}; vector<edge> g[2500];map<string,int> mp;int N,m,k,level[2500],iter[2500];char s[50],a[50],b[50];void    Add_edge (int u,int v,int cap) {G[u].push_back (Edge{v,cap,g[v].size ()}); G[v].push_back (Edge{u,0,g[u].size ()-1});    void BFs (int s) {queue<int> q;    Q.push (s);    Level[s]=1;        while (Q.size ()) {int Now=q.front (); Q.pop ();       for (int i=0;i<g[now].size (); i++) if (g[now][i].cap>0) {     Edge E=g[now][i];                  if (level[e.to]<0) {level[e.to]=level[now]+1;              Q.push (e.to);    }}}}int dfs (int s,int t,int minn) {if (s==t) return Minn;        for (int &i=iter[s];i<g[s].size (); i++) {Edge &e=G[s][i];            if (level[e.to]>level[s]&&e.cap>0) {int K=dfs (e.to,t,min (Minn,e.cap));                 if (k>0) {e.cap-=k;                 G[e.to][e.rev].cap+=k;             return k; }}} return 0;}    int max_flow (int s,int t) {int ans=0,temp;    for (;;)        {memset (level) (level,-1,sizeof);        BFS (s);        if (level[t]<0) return ans;        memset (Iter,0,sizeof (ITER));    while ((Temp=dfs (S,t,inf)) >0) ans+=temp; } return ans;    void Init () {mp.clear (); for (int i=1;i<=2000;i++) g[i].clear ();}    void build () {int num=0; for (int i=1;i<=n;i++) {int u;        scanf ("%s", s);        Mp[s]=++num;    Add_edge (0,mp[s],1);    } scanf ("%d", &m);        for (int i=1;i<=m;i++) {scanf ("%s%s", A, b);        Mp[a]=++num;        if (!mp[b]) mp[b]=++num;        Add_edge (mp[b],mp[a],1);    Add_edge (mp[a],2000,1);    } scanf ("%d", &k);        for (int i=1;i<=k;i++) {scanf ("%s%s", A, b);        if (!mp[a]) mp[a]=++num;        if (!mp[b]) mp[b]=++num;    Add_edge (Mp[b],mp[a],inf); }}void solve () {printf ("%d\n", M-max_flow (0,2000));}        int main () {while (~SCANF ("%d", &n)) {init ();        Build ();    Solve (); } return 0;}

Analysis: Out of the very slag of a problem, it is clear that the maximum flow naked problem, the result of the question of half a day is ambiguous ambiguity,

Max Stream Bare topic, the only thing that is harvested is that the map uses map<string,int> MP to save the string very conveniently, directly scanf ("%s", s) and then mp[s] to read the number of the same string as s in MP

Then mp[]

POJ 1087 Max Stream bare topic + 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.