POJ1087 A Plug for UNIX 2017-02-12 13:38 40人阅读 评论(0) 收藏
Description
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
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
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset> using namespace std; #define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 500 struct node
{
int u, v, next, cap;
} edge[MAXN*MAXN];
int nt[MAXN], s[MAXN], d[MAXN], visit[MAXN];
int cnt;
int ct; void init()
{
cnt = 0;
memset(s, -1, sizeof(s));
} void add(int u, int v, int c)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].cap = c;
edge[cnt].next = s[u];
s[u] = cnt++;
edge[cnt].u = v;
edge[cnt].v = u;
edge[cnt].cap = 0;
edge[cnt].next = s[v];
s[v] = cnt++;
} bool BFS(int ss, int ee)
{
memset(d, 0, sizeof d);
d[ss] = 1;
queue<int>q;
q.push(ss);
while (!q.empty())
{
int pre = q.front();
q.pop();
for (int i = s[pre]; ~i; i = edge[i].next)
{
int v = edge[i].v;
if (edge[i].cap > 0 && !d[v])
{
d[v] = d[pre] + 1;
q.push(v);
}
}
}
return d[ee];
} int DFS(int x, int exp, int ee)
{
if (x == ee||!exp) return exp;
int temp,flow=0;
for (int i = nt[x]; ~i ; i = edge[i].next, nt[x] = i)
{
int v = edge[i].v;
if (d[v] == d[x] + 1&&(temp = (DFS(v, min(exp, edge[i].cap), ee))) > 0)
{
edge[i].cap -= temp;
edge[i ^ 1].cap += temp;
flow += temp;
exp -= temp;
if (!exp) break;
}
}
if (!flow) d[x] = 0;
return flow;
} int Dinic_flow(int ss, int ee)
{
int ans = 0;
while (BFS(ss, ee))
{
for (int i = 0; i < ct; i++) nt[i] = s[i];
ans+= DFS(ss, INF, ee);
}
return ans;
} int main()
{
int n,m,x;
string a,b;
while(~scanf("%d",&n))
{
map<string,int>mp;
init();
ct=2;
for(int i=0; i<n; i++)
{
cin>>a;
mp[a]=ct++;
add(0,mp[a],1);
}
scanf("%d",&m);
for(int i=0; i<m; i++)
{
cin>>a>>b;
mp[a]=ct++;
add(mp[a],1,1);
if(!mp[b])
mp[b]=ct++;
add(mp[b],mp[a],1);
}
scanf("%d",&x);
for(int i=0; i<x; i++)
{
cin>>a>>b;
if(!mp[a])
mp[a]=ct++;
if(!mp[b])
mp[b]=ct++;
add(mp[b],mp[a],INF);
}
printf("%d\n",m-Dinic_flow(0,1));
}
return 0;
}
POJ1087 A Plug for UNIX 2017-02-12 13:38 40人阅读 评论(0) 收藏的更多相关文章
- HDU1072 Nightmare(BFS) 2016-07-24 14:02 40人阅读 评论(0) 收藏
Nightmare Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth w ...
- A Plug for UNIX 分类: POJ 图论 函数 2015-08-10 14:18 2人阅读 评论(0) 收藏
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14786 Accepted: 4994 Desc ...
- Safecracker 分类: HDU 搜索 2015-06-25 21:12 12人阅读 评论(0) 收藏
Safecracker Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- ios UIKit动力 分类: ios技术 2015-07-14 12:55 196人阅读 评论(0) 收藏
UIkit动力学是UIkit框架中模拟真实世界的一些特性. UIDynamicAnimator 主要有UIDynamicAnimator类,通过这个类中的不同行为来实现一些动态特性. 它一般有两种初始 ...
- iOS开发之监听键盘高度的变化 分类: ios技术 2015-04-21 12:04 233人阅读 评论(0) 收藏
最近做的项目中,有一个类似微博中的评论转发功能,屏幕底端有一个输入框用textView来做,当textView成为第一响应者的时候它的Y值随着键盘高度的改变而改变,保证textView紧贴着键盘,但又 ...
- POJ1789 Truck History 2017-04-13 12:02 33人阅读 评论(0) 收藏
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27335 Accepted: 10634 D ...
- ZOJ3770Ranking System 2017-04-14 12:42 52人阅读 评论(0) 收藏
Ranking System Time Limit: 2 Seconds Memory Limit: 65536 KB Few weeks ago, a famous software co ...
- leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏
for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...
- Maya Calendar 分类: POJ 2015-06-11 21:44 12人阅读 评论(0) 收藏
Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 70016 Accepted: 21547 D ...
随机推荐
- Bootstrap学习之路(1)---开篇-登陆页
Bootstrap是现在很流行的一套前端框架,尤其是它的自适应,真的很不错,而且对移动设备也很友好,可以达到快速开发的效果,最近想把自己的网站弄个手机版,很果断的就选用了bootstrap,跟大家分享 ...
- RDLC报表系列一
1.报表项目搭建: 配置好后,单击Web服务URL:http://lg-20151517ryre/ReportServer 如果电脑系统打开的时候没有设置密码的话,此时打开有可能会出现需要登录名和密码 ...
- LAMP的安装和注意事项
LAMP--Linux+Apache(httpd)+MySQL+PHP,是常用的web服务器架构,下面接受编译安装的过程,以及出现的错误. 注意事项: 1. 扩展epel源:参照:http://www ...
- mysql-11元数据
我的mysql是用docker配置的,服务器状态和服务器配置变量都未设置,报错正常.
- Hive 体系结构
1.Hive架构与基本组成 下面是Hive的架构图. 图1.1 Hive体系结构 Hive的体系结构可以分为以下几部分: (1)用户接口主要有三个:CLI,Client 和 W ...
- MySQL 5.7 免安装版配置
MySQL5.7免安装版配置 Mysql是一个比较流行且很好用的一款数据库软件,如下记录了我学习总结的mysql免安装版的配置经验. 一. 软件下载 5.7 32位https://dev.mysq ...
- SQL 基础命令和函数
[数据操作] SELECT --从数据库表中检索数据行和列 INSERT --向数据库表添加新数据行 DELETE --从数据库表中删除数据行 UPDATE --更新数据库表中的数据 [数据定义] C ...
- 发布spring jar包, 报错
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Una ...
- 2015年传智播客JavaEE 第168期就业班视频教程day45-ERP项目-01 05-主线流程
采购管理 销售管理 采购退货管理 销售退货管理 老师的采购单的审核是分三级审核的,如果这个单子是个普通单子,那么审核一下就完了,如果这个单子超过100万,需要二级领导审核,如果这个单子超过500万, ...
- 130. Surrounded Regions (Graph; DFS)
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...