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

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

  1. 4
  2. A
  3. B
  4. C
  5. D
  6. 5
  7. laptop B
  8. phone C
  9. pager B
  10. clock B
  11. comb X
  12. 3
  13. B X
  14. X A
  15. X D

Sample Output

  1. 1
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <vector>
  7. #include <queue>
  8. #include <map>
  9. #include <algorithm>
  10. #include <set>
  11. using namespace std;
  12. #define MM(a,b) memset(a,b,sizeof(a))
  13. typedef long long ll;
  14. typedef unsigned long long ULL;
  15. const int mod = 1000000007;
  16. const double eps = 1e-10;
  17. const int inf = 0x3f3f3f3f;
  18. const int big=50000;
  19. int max(int a,int b) {return a>b?a:b;};
  20. int min(int a,int b) {return a<b?a:b;};
  21. struct edge{
  22. int to,cap,rev;
  23. };
  24.  
  25. vector<edge> G[2500];
  26. map<string,int> mp;
  27. int n,m,k,level[2500],iter[2500];
  28. char s[50],a[50],b[50];
  29. void add_edge(int u,int v,int cap)
  30. {
  31. G[u].push_back(edge{v,cap,G[v].size()});
  32. G[v].push_back(edge{u,0,G[u].size()-1});
  33. }
  34.  
  35. void bfs(int s)
  36. {
  37. queue<int> q;
  38. q.push(s);
  39. level[s]=1;
  40. while(q.size())
  41. {
  42. int now=q.front();q.pop();
  43. for(int i=0;i<G[now].size();i++)
  44. if(G[now][i].cap>0)
  45. {
  46. edge e=G[now][i];
  47. if(level[e.to]<0)
  48. {
  49. level[e.to]=level[now]+1;
  50. q.push(e.to);
  51. }
  52. }
  53. }
  54. }
  55. int dfs(int s,int t,int minn)
  56. {
  57. if(s==t)
  58. return minn;
  59. for(int &i=iter[s];i<G[s].size();i++)
  60. {
  61. edge &e=G[s][i];
  62. if(level[e.to]>level[s]&&e.cap>0)
  63. {
  64. int k=dfs(e.to,t,min(minn,e.cap));
  65. if(k>0)
  66. {
  67. e.cap-=k;
  68. G[e.to][e.rev].cap+=k;
  69. return k;
  70. }
  71. }
  72. }
  73. return 0;
  74. }
  75.  
  76. int max_flow(int s,int t)
  77. {
  78. int ans=0,temp;
  79. for(;;)
  80. {
  81. memset(level,-1,sizeof(level));
  82. bfs(s);
  83. if(level[t]<0)
  84. return ans;
  85. memset(iter,0,sizeof(iter));
  86. while((temp=dfs(s,t,inf))>0)
  87. ans+=temp;
  88. }
  89. return ans;
  90. }
  91.  
  92. void init()
  93. {
  94. mp.clear();
  95. for(int i=1;i<=2000;i++) G[i].clear();
  96. }
  97.  
  98. void build()
  99. {
  100. int num=0;
  101. for(int i=1;i<=n;i++)
  102. {
  103. int u;
  104. scanf("%s",s);
  105. mp[s]=++num;
  106. add_edge(0,mp[s],1);
  107. }
  108. scanf("%d",&m);
  109. for(int i=1;i<=m;i++)
  110. {
  111. scanf("%s %s",a,b);
  112. mp[a]=++num;
  113. if(!mp[b]) mp[b]=++num;
  114. add_edge(mp[b],mp[a],1);
  115. add_edge(mp[a],2000,1);
  116. }
  117. scanf("%d",&k);
  118. for(int i=1;i<=k;i++)
  119. {
  120. scanf("%s %s",a,b);
  121. if(!mp[a]) mp[a]=++num;
  122. if(!mp[b]) mp[b]=++num;
  123. add_edge(mp[b],mp[a],inf);
  124. }
  125. }
  126.  
  127. void solve()
  128. {
  129. printf("%d\n",m-max_flow(0,2000));
  130. }
  131.  
  132. int main()
  133. {
  134. while(~scanf("%d",&n))
  135. {
  136. init();
  137. build();
  138. solve();
  139. }
  140. return 0;
  141. }

  分析:出的很渣的一道题,明明是道最大流裸题,结果题目讲的半天都含糊不清有歧义,,

最大流裸题,,唯一有收获的是map的使用map<string,int> mp保存字符串非常方便,直接scanf("%s",s)   然后mp[s]就可以读出mp中与s相同的字符串的个数

然后mp[]

POJ 1087 最大流裸题 + map的更多相关文章

  1. hdu Flow Problem (最大流 裸题)

    最大流裸题,贴下模版 view code#include <iostream> #include <cstdio> #include <cstring> #incl ...

  2. POJ 3468 线段树裸题

    这些天一直在看线段树,因为临近期末,所以看得断断续续,弄得有些知识点没能理解得很透切,但我也知道不能钻牛角尖,所以配合着刷题来加深理解. 然后,这是线段树裸题,而且是最简单的区间增加与查询,我参考了A ...

  3. Going Home POJ - 2195 费用流板子题

    On a grid map there are n little men and n houses. In each unit time, every little man can move one ...

  4. POJ 1258 + POJ 1287 【最小生成树裸题/矩阵建图】

    Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet c ...

  5. poj 1087 最大流

    没啥好说的,慢慢建图 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 Out ...

  6. HDU 3376 &amp;&amp; 2686 方格取数 最大和 费用流裸题

    题意: 1.一个人从[1,1] ->[n,n] ->[1,1] 2.仅仅能走最短路 3.走过的点不能再走 问最大和. 对每一个点拆点限流为1就可以满足3. 费用流流量为2满足1 最大费用流 ...

  7. HDU3549 最大流 裸题

    EK算法 时间复杂度o(n*m*m)  因为有反向边每次bfs时间为 n*m 每次删一条边 最多m次 代码 #include<iostream> #include<string.h& ...

  8. POJ 1459 最大流 第二题

    http://poj.org/problem?id=1459 也是网络流的基础,只是虚拟出一个源点和终点,对应的生产值和消费值就加到与源点和终点的边上,然后做一次bfs就好了. #include &l ...

  9. 紫书 习题 11-3 UVa 820 (最大流裸题)

    注意这道题是双向边, 然后直接套模板就ok了. #include<cstdio> #include<algorithm> #include<vector> #inc ...

随机推荐

  1. SpringMVC异常体系

    在服务端经常会遇到需要手动的抛出异常,比如业务系统,校验异常,比较通用的处理方案是在最顶层进行拦截异常,例如Struts的全局异常处理,而Spring的异常处理机制就相对于Struts来说好用多了   ...

  2. CSUST 2012 一个顶俩 (本校OJ题)(思维+树链剖分)

    (点击这里查看原题,不保证可以进去....外网可能比较卡) Description A:一心一意 B:一个顶俩 最近QQ更新后那个成语接龙好像挺火的?但我只知道图论里一条边是一个顶俩个点的emm. 如 ...

  3. layui2.5 修改layuicms

    雷哥layui2.5版本学习 学习地址: https://www.bilibili.com/video/av59813890/?p=30 注意: 修改layuicms时注意下面是缓存的js, < ...

  4. Ubuntu14.04安装Caffe(CPU)

    一 安装Ubuntu14.04LTS Ubuntu分区 1.SWAP 交换分区:与物理内存相当. 2.“/” 根目录分区:该区大小由硬盘大小而定,10-100G. 3.“HOME” 家目录分区:该区也 ...

  5. iView 发布后台管理系统 iview-admin

    简介 iView Admin 是基于 Vue.js,搭配使用 iView UI 组件库形成的一套后台集成解决方案,由 TalkingData 前端可视化团队部分成员开发维护.iView Admin 遵 ...

  6. Nginx负载均衡调度算法

    Nginx支持的负载均衡调度算法方式如下: 1. weight轮询(默认) 接收到的请求按照顺序逐一分配到不同的后端服务器,即使在使用过程中,某一台后端服务器宕机,nginx会自动将该服务器剔除出队列 ...

  7. less 经典范例 bootstrap 的 less 版本 常用 less 代码

    1. bootstrap 的 less 版本 2.less 文件分布 /*! * Bootstrap v3.3.7 (http://getbootstrap.com) * Copyright 2011 ...

  8. centos7搭建activemq服务

    一.下载安装jdk 下载 jdk-8u211-linux-x64.rpm安装: yum -y install jdk-8u211-linux-x64.rpm 二.官网下载 activemq 软件包 官 ...

  9. python分别获取虚拟网卡和真实网卡ip

    #!/usr/bin/python # -*- coding: utf-8 -*- import commands import socket import fcntl import struct C ...

  10. java8学习之Collectors工厂类源码分析与实战

    如上一节[http://www.cnblogs.com/webor2006/p/8360232.html]在结尾处谈到的,彻底理解了Collector收集器之后,有必要对其系统Collectors实现 ...