题目链接

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

AC

  • 建图:

    1. 将源点和电器需要插座建边,权值为需要的个数
    2. 将插座转换器建边,权值为INF(商店可以提供无数个)
    3. 将已有的插座和汇点相连,权值为该种类插座的数目
  • 注意:

    1. 如果用邻接矩阵写,矩阵最小开到400,因为插座的种类最坏的情况是400种,已存在的插座100种,需要的插座100种,转换器100 + 100 = 200 种,所以最多400种
  • 代码后面有极限数据

#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#define N 300005
#include <cstring>
#define ll long long
#define P pair<int, int>
#define mk make_pair
using namespace std;int pre[405], a[405][405];
bool vis[405];
int inf = 0x3f3f3f3f;
int n, m, k;
// EK最大流
bool bfs(int s, int e) {memset(pre, -1, sizeof(pre));memset(vis, false, sizeof(vis));vis[s] = true;pre[s] = s;queue<int> que;que.push(s);while (!que.empty()) {int t = que.front();que.pop();for (int i = 0; i <= e; ++i) {if (vis[i] || a[t][i] == 0) continue;vis[i] = true;pre[i] = t;if (i == e) return true;que.push(i);}}return false;
}
ll solve(int s, int e) {ll sum = 0;while (bfs(s, e)) {int MIN = inf;for (int i = e; i != s; i = pre[i]) {MIN = min(MIN, a[pre[i]][i]);}for (int i = e; i != s; i = pre[i]) {a[pre[i]][i] -= MIN;a[i][pre[i]] += MIN;}sum += MIN;}return sum;
}int main() {
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);
#endifios::sync_with_stdio(false);while (cin >> n) {memset(a, 0, sizeof(a));// 统计现有的插座种类和数目 map<string, int> mp1;// 统计电器插头种类和数目 map<string, int> mp2;// 对所有插座和插头进行标记 map<string, int> mp;int now = 1;// 读入插座 for (int i = 0; i < n; ++i) {string s;cin >> s;mp1[s]++;if (mp[s] == 0) mp[s] = now++;}// 读入插头 cin >> m;for (int i = 0; i < m; ++i) {string s;cin >> s >> s;mp2[s]++; if (mp[s] == 0) mp[s] = now++;}// 将源点和电器需要的插座相连,权值为需要的个数 map<string, int>::iterator it;for (it = mp2.begin(); it != mp2.end(); ++it) {a[0][mp[it->first]] = it->second;}// 读入转换器 cin >> k;for (int i = 0; i < k; ++i) {string s1, s2;cin >> s1 >> s2;if (mp[s1] == 0)    mp[s1] = now++;if (mp[s2] == 0)    mp[s2] = now++;// 因为商店可以提供无数个,权值设为 inf a[mp[s1]][mp[s2]] = inf;}// 将现有的插座和汇点相连 for (it = mp1.begin(); it != mp1.end(); ++it) {a[mp[it->first]][401] = it->second;}// 插座和插头最多400种,已有的100种,需要的100种,转换器100 + 100 ll ans = solve(0, 401);printf("%d\n", m - ans);}return 0;
}
  • 数据
100
a0
a1
a2
a3
a4
a5
a6
a7
a8
a9
a10
a11
a12
a13
a14
a15
a16
a17
a18
a19
a20
a21
a22
a23
a24
a25
a26
a27
a28
a29
a30
a31
a32
a33
a34
a35
a36
a37
a38
a39
a40
a41
a42
a43
a44
a45
a46
a47
a48
a49
a50
a51
a52
a53
a54
a55
a56
a57
a58
a59
a60
a61
a62
a63
a64
a65
a66
a67
a68
a69
a70
a71
a72
a73
a74
a75
a76
a77
a78
a79
a80
a81
a82
a83
a84
a85
a86
a87
a88
a89
a90
a91
a92
a93
a94
a95
a96
a97
a98
a99
100
b0 c0
b1 c1
b2 c2
b3 c3
b4 c4
b5 c5
b6 c6
b7 c7
b8 c8
b9 c9
b10 c10
b11 c11
b12 c12
b13 c13
b14 c14
b15 c15
b16 c16
b17 c17
b18 c18
b19 c19
b20 c20
b21 c21
b22 c22
b23 c23
b24 c24
b25 c25
b26 c26
b27 c27
b28 c28
b29 c29
b30 c30
b31 c31
b32 c32
b33 c33
b34 c34
b35 c35
b36 c36
b37 c37
b38 c38
b39 c39
b40 c40
b41 c41
b42 c42
b43 c43
b44 c44
b45 c45
b46 c46
b47 c47
b48 c48
b49 c49
b50 c50
b51 c51
b52 c52
b53 c53
b54 c54
b55 c55
b56 c56
b57 c57
b58 c58
b59 c59
b60 c60
b61 c61
b62 c62
b63 c63
b64 c64
b65 c65
b66 c66
b67 c67
b68 c68
b69 c69
b70 c70
b71 c71
b72 c72
b73 c73
b74 c74
b75 c75
b76 c76
b77 c77
b78 c78
b79 c79
b80 c80
b81 c81
b82 c82
b83 c83
b84 c84
b85 c85
b86 c86
b87 c87
b88 c88
b89 c89
b90 c90
b91 c91
b92 c92
b93 c93
b94 c94
b95 c95
b96 c96
b97 c97
b98 c98
b99 c99
100
d0 e0
d1 e1
d2 e2
d3 e3
d4 e4
d5 e5
d6 e6
d7 e7
d8 e8
d9 e9
d10 e10
d11 e11
d12 e12
d13 e13
d14 e14
d15 e15
d16 e16
d17 e17
d18 e18
d19 e19
d20 e20
d21 e21
d22 e22
d23 e23
d24 e24
d25 e25
d26 e26
d27 e27
d28 e28
d29 e29
d30 e30
d31 e31
d32 e32
d33 e33
d34 e34
d35 e35
d36 e36
d37 e37
d38 e38
d39 e39
d40 e40
d41 e41
d42 e42
d43 e43
d44 e44
d45 e45
d46 e46
d47 e47
d48 e48
d49 e49
d50 e50
d51 e51
d52 e52
d53 e53
d54 e54
d55 e55
d56 e56
d57 e57
d58 e58
d59 e59
d60 e60
d61 e61
d62 e62
d63 e63
d64 e64
d65 e65
d66 e66
d67 e67
d68 e68
d69 e69
d70 e70
d71 e71
d72 e72
d73 e73
d74 e74
d75 e75
d76 e76
d77 e77
d78 e78
d79 e79
d80 e80
d81 e81
d82 e82
d83 e83
d84 e84
d85 e85
d86 e86
d87 e87
d88 e88
d89 e89
d90 e90
d91 e91
d92 e92
d93 e93
d94 e94
d95 e95
d96 e96
d97 e97
d98 e98
d99 e99

POJ 1087 -- A Plug for UNIX(最大流,建图)(文末有极限数据)相关推荐

  1. POJ - 1087 A Plug for UNIX(最大流)

    题目链接:点击查看 题目大意:给出n个互不相同的设备,m个插座以及k种适配器,每种适配器都有无限个,适配器可以互相搭配,问如何匹配可以让尽可能多的设备用上电 题目分析:裸的最大流,就是加上了个字符串把 ...

  2. POJ 1087 A Plug for UNIX 会议室插座问题 构图+最大流

    题目链接:POJ 1087 A Plug for UNIX A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  3. poj 1087 A Plug for UNIX 【最大流】

    题目连接:http://poj.org/problem? id=1087 题意: n种插座 ,m个电器,f组(x,y)表示插座x能够替换插座y,问你最多能给几个电器充电. 解法:起点向插座建边,容量1 ...

  4. poj 1087 A Plug for UNIX

    题目描述:现在由你负责布置Internet联合组织首席执行官就职新闻发布会的会议室.由于会议室修建时被设计成容纳全世界各地的新闻记者,因此会议室提供了多种电源插座用以满足(会议室修建时期)各国不同插头 ...

  5. POJ 1149 最大流建图 PIGS

    题意: 给出猪圈个数 m 和买家人数 n 然后给出m个猪圈的猪的头数.. 接下来 n 行.. 给出mm a1 a2 .. a(mm) k 例如 2 1 5 3 表示第i+1个用户 有mm(2) 个猪圈 ...

  6. Codeforces 362E Petya and Pipes 费用流建图

    题意: 给一个网络中某些边增加容量,增加的总和最大为K,使得最大流最大. 费用流:在某条边增加单位流量的费用. 那么就可以2个点之间建2条边,第一条给定边(u,v,x,0)这条边费用为0 同时另一条边 ...

  7. BZOJ4205卡牌配对——最大流+建图优化

    题目描述 现在有一种卡牌游戏,每张卡牌上有三个属性值:A,B,C.把卡牌分为X,Y两类,分别有n1,n2张. 两张卡牌能够配对,当且仅当,存在至多一项属性值使得两张卡牌该项属性值互质,且两张卡牌类别不 ...

  8. 洛谷 P2071 座位安排 (最大流 + 建图)

    2020.7.15 今天先开个小差,这图书馆接近零下的气温给爷冻傻了,这还咋写题?马上回去了,练一练网络和tarjan吧. 这道题很简单,问有2n个人,n排座位,每个人都有喜欢的两个座位,最多能安置多 ...

  9. POJ 1511 Invitation Cards——Dijkstra优先队列优化+反向建图

    [题目描述] In the age of television, not many people attend theater performances. Antique Comedians of M ...

最新文章

  1. django学习教程
  2. python的print函数
  3. Flask之扩展flask-migrate
  4. 10月25日lol服务器维护,《LOL》lol10月25日停机维护到什么时候 10.25维护结束时间...
  5. SpringBoot打包时提示:Perhaps you are running on a JRE rather than a JDK?
  6. 解决VS2013无法安装ArcObjects10.2的问题
  7. hive几种执行sql的方式总结
  8. PHP登录表单提交前端验证,form表单提交前先用ajax进行验证(前端)
  9. 农银电商项目学习笔记(一)
  10. Java泛型原理、类型擦除
  11. Python3安装Crypto模块
  12. 微信表情包储服务器,新发现!微信里的表情包,终于能保存到手机和电脑辣!-qq表情在哪个文件夹里...
  13. 啦啦外卖UNIAPP(4.0)源码商家+骑手
  14. 咖说丨去中心化借贷的逻辑和商业基础
  15. Centos 8.5系统优化方案
  16. 华为路由器6to4隧道原理及配置
  17. 跟偶一起做:击退眼睛疲劳的五大运动
  18. 羊了个羊的模式浅薄认知
  19. 2022最新python100个实战练手项目,【附源码】,快来学习起来吧!
  20. hdu2838(树状数组)

热门文章

  1. 动态密码卡TOTP算法
  2. Java 并发专题 :FutureTask 实现预加载数据 在线看电子书、浏览器浏览网页等
  3. ASP.NET MVC 动态选择VIEW
  4. SmtpClient 身份验证失败(authentication failed) 的原因分析
  5. iOS之CocoaPods二进制化的实现方案
  6. 对于窗口大小为n个滑动窗口,最多可以有( )帧已发送但没有确认。
  7. 《每日一题》49. Group Anagrams 字母异位词分组
  8. 阿里面试官问我,你们的需求研发/开发流程是怎样的?我???
  9. python人工智能——机器学习——分类算法-朴素贝叶斯算法对新闻进行分类案例
  10. 【机器视觉】 deserialize_measure算子