poj1778 All Discs Considered
思路:
拓扑排序。贪心。
实现:
#include <bits/stdc++.h>
using namespace std;
vector<int> G[];
int n1, n2;
inline void help(queue<int>& a, queue<int>& b, int x)
{
if (x > n1) b.push(x);
else a.push(x);
}
bool same(int x, int y)
{
if (x >= && x <= n1 && y >= && y <= n1) return true;
if (x > n1 && y > n1) return true;
return false;
}
int cal(queue<int> a, queue<int> b, vector<int>in, bool set)
{
int cnt = , last = -, rnd = ;
while (!a.empty() || !b.empty())
{
if ((!set && rnd) || set)
{
while (!a.empty() && !in[a.front()])
{
int tmp = a.front(); a.pop();
if (!same(tmp, last)) cnt++;
last = tmp;
for (int i = ; i < (int)G[tmp].size(); i++)
{
int son = G[tmp][i];
if (--in[son] == ) help(a, b, son);
}
}
}
while (!b.empty() && !in[b.front()])
{
int tmp = b.front(); b.pop();
if (!same(tmp, last)) cnt++;
last = tmp;
for (int i = ; i < (int)G[tmp].size(); i++)
{
int son = G[tmp][i];
if (--in[son] == ) help(a, b, son);
}
}
rnd++;
}
return cnt;
}
int main()
{
int d, x, y;
while (~scanf("%d %d %d", &n1, &n2, &d), n1 + n2 + d)
{
for (int i = ; i <= n1 + n2; i++) G[i].clear();
vector<int> in(n1 + n2 + , );
while (d--)
{
scanf("%d %d", &x, &y);
G[y].push_back(x);
in[x]++;
}
queue<int> a, b;
for (int i = ; i <= n1 + n2; i++)
{
if (!in[i]) help(a, b, i);
}
printf("%d\n", min(cal(a, b, in, true), cal(a, b, in, false)) + );
}
return ;
}
poj1778 All Discs Considered的更多相关文章
- POJ 1778 All Discs Considered(拓扑排序)
点我看题目 题意 :其实题意我也说不清楚,因为比赛的时候我盯着看了1个小时也没看懂....就是两个磁盘,第一个有n1的安装包,编号为1~n1,第二个有n2个安装包,编号为n1~n2.给你d对关系,(x ...
- All Discs Considered(拓扑排序)
http://poj.org/problem?id=1778 题意:有两个DVD,第一个DVD上有编号为1~n1的安装包,第二个DVD上有编号为n1+1~n1+n2的安装包,给出m组关系(a,b) 表 ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- BAYESIAN STATISTICS AND CLINICAL TRIAL CONCLUSIONS: WHY THE OPTIMSE STUDY SHOULD BE CONSIDERED POSITIVE(转)
Statistical approaches to randomised controlled trial analysis The statistical approach used in the ...
- [DEPRECATION] Encountered positional parameter near xxx Positional parameter are considered deprecated; use named parameters or JPA-style positional parameters instead.
WARN:30 20:55:45,340ms- [HqlSqlWalker]1009行-[DEPRECATION] Encountered positional parameter near line ...
- Exception Handling Considered Harmful
异常处理被认为存在缺陷 Do, or do not. There is no try. - Yoda, The Empire Strikes Back (George Lucas) by Jason ...
- Which ports are considered unsafe on Chrome
1, // tcpmux 7, // echo 9, // discard 11, // systat 13, // daytime 15, // netstat 17, // qotd 19, // ...
- 最小圆覆盖(Smallest Enclosing Discs)
随机增量算法(a randomized incremental algorithm) #define sqr(x) ((x)*(x)) #define EPS 1e-4 struct P{ doubl ...
随机推荐
- Ubuntu 16.04系统启动时卡在:(initramfs)
背景: 由于不正常的关机和重启,或者突然断电导致的关机,下次起来后不能进去系统,停留在(initramfs). 解决方法: 使用如下命令修复 fsck -y /dev/sda1 说明:其中sda1为系 ...
- Nginx教程收集
学习要系统,最推荐的方式是看书. 下面是收集的一些Nginx教程: https://www.gitbook.com/book/yinsigan/nginx/details http://www.ngi ...
- struts2学习笔记(二)—— 获取登录信息及计算在线人数
实现目的: 1.点击"Login"的超链接.进入登录页面 2.填写登录信息.提交表单,将用户信息保存进Session 3.显示用户名,并计算在线 ...
- Linux学习系列之MySQL备份
MySQL排除表备份 #!/bin/bash #created by 90root #date: 20160809 date_y=$(date +%Y) date_m=$(date +%m) time ...
- 装机、做系统必备:秒懂MBR和GPT分区表
从Intel 6系列主板之后,就开始提供UEFI BIOS支持,正式支持GPT硬盘分区表,一举取代了此前的MBR分区表格式,不过为了保持对老平台的兼容,微软即使最新的Windows 10系统也继续提供 ...
- luajit利用ffi结合C语言实现面向对象的封装库
luajit中.利用ffi能够嵌入C.眼下luajit的最新版是2.0.4,在这之前的版本号我还不清楚这个扩展库详细怎么样,只是在2.04中,真的非常爽. 既然是嵌入C代码.那么要说让lua支持 ...
- hdu 4869 Turn the pokers 策略(组合数)
题意:输入操作次数n和扑克牌数m,一開始扑克牌全都背面朝上. 如今输入n个数xi,表示选择xi张牌翻转,问最后的牌的情况有多少种可能? 题解: 我们将一開始的牌觉得是m个0.而翻转就是将0变成1或者1 ...
- .a 文件解析
首先先准备一个静态库.a文件,比如叫staticLibrary.a,放在桌面的test目录里. 分离arch 首先先file一下staticLibrary.a,看一下该文件包含几种arch. ~ cd ...
- ssh 远程登陆指定port
ssh 到指定port ssh -p xx user@ip xx 为 port号 user为username ip为要登陆的ip
- Thinkphp中使用PHPmailer发送邮件
在ThinkPHP\Extend\Vendor\目录下放入PHPMailer文件夹,里面包含以下文件 重置密码发送邮件 public function recover(){ if($this-> ...