POJ 1087

来源:互联网 发布:lol比赛数据查询 编辑:程序博客网 时间:2024/05/19 16:29

A Plug for UNIX
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 17824 Accepted: 6150

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 receptacles 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 many years ago when reporters used very few electric and electronic devices and is equipped 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 is likely to be long and tedious, you want to be able to plug in as many as you can. 
Before the meeting begins, you gather up all the devices that the reporters would like to use, and attempt to set them up. You notice that some of the devices use plugs for which there is no receptacle. You wonder if these devices are from countries that didn't exist when the room was built. For some receptacles, there are several devices that use the corresponding plug. For other receptacles, 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 essentially 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 receptacles in the room. The next n lines list the receptacle types found in the room. Each receptacle 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 would like to plug in. Each of the next m lines lists the name of a device followed by the type of plug it uses (which is identical to the type of receptacle 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 receptacle 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

Source

East Central North America 1999

[Submit]   [Go Back]   [Status]   [Discuss]


题意:给出设备和插座,每个插座只有一个位置和插头,每个设备都只有一个插头,且只能插对应的插座,最后有k种适配器可以选择,可以将插头转换为另外一种插头;


思路:最大流,这里我用Dinic



Ac code:

#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <queue>#include <map>#include <iostream>using namespace std;const int INF =0x3f3f3f3f;const int N=1000+50;map<string ,int> map1;struct Edge {    int from,to,cap,flow;};vector<Edge> edge;vector<int> G[N];int pre[N];//记录路径int vis[N];//起点到i的可改进量bool bfs(int s,int t) {    memset(vis,0,sizeof(vis));    queue<int>q;    q.push(s);    vis[s]=INF;    while(!q.empty()) {//从源点出发寻找        int x=q.front(); q.pop();        int len=G[x].size();        for(int i=0; i<len; i++) {            Edge& e=edge[G[x][i]];            if(!vis[e.to]&&e.cap>e.flow) {                vis[e.to]=min(vis[x],e.cap-e.flow);                pre[e.to]=G[x][i];                q.push(e.to);            }        }    }    return vis[t];}int max_flow(int s,int t) {    int flow=0;    while(bfs(s,t)) {        for(int u=t; u!=s; u=edge[pre[u]].from) {            edge[pre[u]].flow+=vis[t];            edge[pre[u]^1].flow-=vis[t];        }        flow+=vis[t];    }    return flow;}void add_edge(int from,int to,int cap) {    edge.push_back((Edge) {from,to,cap,0});    edge.push_back((Edge) {to,from,0,0});    int m=edge.size();    G[from].push_back(m-2);    G[to].push_back(m-1);}void init(int n) {    for(int i=0; i<=n; i++) {        G[i].clear();    }    edge.clear();    memset(pre,0,sizeof pre);    // memset(vis,0,sizeof vis);}int main() {    //ios::sync_with_stdio(false);    int n;    while(~scanf("%d",&n)){        int st=0;        int ter=N-1;//vis只开到N-1        int plugNum=1;        init(n*2);        char s[30];        while(n--){            scanf("%s",s);            map1[s]=plugNum++;            add_edge(map1[s],ter,1);                }        int m; scanf("%d",&m);        int ans=m;        char k[30],v[30];        while(m--){            scanf("%s%s",k,v);            if(!map1[v]) {//新插头            map1[v]=plugNum++;            }            map1[k]=plugNum++;            add_edge(map1[k],map1[v],1);    //设备指向插头            add_edge(st,map1[k],1);              }        int q;        cin>>q;        char u[30];        while(q--){            scanf("%s%s",u,v);            if(!map1[u]) {                map1[u]=plugNum++;          //新插头            }            if(!map1[v]) {                map1[v]=plugNum++;                    }            add_edge(map1[u],map1[v],INF);  //插头之间无穷        }        ans-=max_flow(st,ter);        printf("%d\n",ans);    }    return 0;}



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 一直显示手机淘宝已停止运行怎么办 唯品会买了不可以退货的衣服怎么办 淘宝店卖东西邮费太贵怎么办 支付宝登录上去必须手机验证怎么办 支付宝里的钱被盗了怎么办 淘宝绑定的支付宝账号忘记了怎么办 支付宝绑定的微博账号忘记了怎么办 怎么知道自己的淘宝密码忘了怎么办 更换扣扣头像图片太大放不下怎么办 每次登入淘宝都要手机验证码怎么办 手机停机收不到唯品会验证码怎么办 魅蓝手机账户密码忘了怎么办 支付宝登录失败请稍后再试怎么办 万王之王3d限制注册了怎么办 绑定手机号无法登陆以前微信怎么办 手机号注册了淘宝如果不用了怎么办 淘宝网登陆要手机验证码怎么办 手机清理了微信图片打不开了怎么办 到淘宝网买东西卖家拒绝退货怎么办 淘宝店铺检测出他人认证图片怎么办 文件过大无法复制到u盘怎么办 淘宝账号上不去总说网络不好怎么办 手机上登录微信一直闪退怎么办 京东账号手机号换了登陆不了怎么办 对方账号停止收款我们汇了款怎么办 淘宝店铺严重违规被扣12分怎么办 中国网银登陆不会自动弹出怎么办 高考报考的时候页面无法显示怎么办 如果卖家收到货不退款怎么办 淘宝退货卖家收到货不退款怎么办 手机号码淘宝被注册跟换绑定怎么办 微信发送ppt显示文件太大怎么办 微信手机号丢了微信登不了钱怎么办 新买的号码注册过支付宝怎么办 新买的号码被注册过支付宝怎么办 支付宝同号码注册了新的帐号怎么办 新卖的号码有人注册过支付宝怎么办 我的号码被别人注册了支付宝怎么办 别人用我的号码注册了支付宝怎么办 打开支付宝进入到淘宝的界面怎么办 我的手机号注销了支付宝账号怎么办