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 ...
随机推荐
- apple applessd.sys error
http://bbs.feng.com/read-htm-tid-10242622.html
- 南阳OJ独木舟上的旅行
/*独木舟上的旅行 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描写叙述 进行一次独木舟的旅行活动.独木舟能够在港口租到,而且之间没有差别. 一条独木舟最多仅仅能乘坐 ...
- jquery验证插件validate自定义扩展
<script src="${pageContext.request.contextPath}/resources/js/jquery-1.12.0.min.js" type ...
- ios打包静态库
1. 什么是库? 所谓库就是程序代码的集合,是共享程序代码的一种方式. 2. 库的分类 根据程序代码的开源情况,库可以分为两类 开源库源代码是公开的,你可以看到具体实现.比如GitHub上比较出名的第 ...
- 复制class文件到as中出现非法字符,须要class,interface货enum
问题如题,出现此情况是在导入eclipse项目到Android Studio出现这种错误, 非法字符: '\ufeff' 解决方式|错误: 须要class, interface或enum,查阅后了解到 ...
- 【C/C++多线程编程之五】pthread线程深入理解
多线程编程之pthread线程深入理解 Pthread是 POSIX threads 的简称,是POSIX的线程标准. 前几篇博客已经能给你初步的多线程概念.在进一步学 ...
- 使用Hibernate防止SQL注入的方法
之前写代码,往后台传入一个组织好的String类型的Hql或者Sql语句,去执行. 这样其实是很蠢的一种做法!!!! 举个栗子~~ 我们模仿一下用户登录的场景: 常见的做法是将前台获取到的用户名和密码 ...
- BZOJ 3041 水叮当的舞步
3041: 水叮当的舞步 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 120 Solved: 67[Submit][Status][Discuss ...
- BZOJ_3667_Rabin-Miller算法_Mille_Rabin+Pollard rho
BZOJ_3667_Rabin-Miller算法_Mille_Rabin+Pollard rho Description Input 第一行:CAS,代表数据组数(不大于350),以下CAS行,每行一 ...
- bzoj4868
http://www.lydsy.com/JudgeOnline/problem.php?id=4868 三分+贪心 我们可以知道这是一个单峰函数 当A>B那么我们每次调整一个的价钱是最佳的,所 ...