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 ...
随机推荐
- MySQL主从复制搭建教程收集(待实践)
先收集一下,后续再搭建测试. https://zhangge.net/4019.html http://www.cnblogs.com/jiangwenju/p/6098974.html http:/ ...
- BZOJ 2208 JSOI2010 连通数 Tarjan+拓扑排序
题目大意:给定一个n个点的有向图,求有多少点对(x,y),使x沿边可到达y 设f[i][j]为从i到j是否可达 首先强联通分量中的随意两个点均可达 于是我们利用Tarjan缩点 缩点之后是一个拓扑图. ...
- HTML5+CSS3设计界面
近期在做一个关于房屋装修的手机上的项目,前台是用H5+C3完毕的,挂在微信上.全部相对来说不是非常难. 这段时间通过敲Html5+Css3.分享一些自己觉得值得学习的知识. 都非常easy.自己操作一 ...
- uva 10765 Doves and Bombs(割顶)
题意:给定一个n个点的连通的无向图,一个点的"鸽子值"定义为将它从图中删去后连通块的个数.求每一个点的"鸽子值". 思路dfs检查每一个点是否为割顶,并标 ...
- opencl+opencv实现sobel算法
这几天在看opencl编程指南.照着书中的样例实现了sobel算法: 1.结合opencv读取图像,保存到缓冲区中. 2.编写和编译内核.并保存显示处理后的结果. 内核: const sampler_ ...
- ZOJ 1414:Number Steps
Number Steps Time Limit: 2 Seconds Memory Limit: 65536 KB Starting from point (0,0) on a plane, ...
- 【Silverlight】Bing Maps学习系列(八):使用Bing Maps Silverlight Control加载自己部署的Google Maps
[Silverlight]Bing Maps学习系列(八):使用Bing Maps Silverlight Control加载自己部署的Google Maps 上个月微软必应地图(Bing Maps) ...
- mac 下idea光标问题
可以使用 fn + i 解决此问题
- poj 2762(tarjan缩点+判断是否是单链)
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19234 ...
- mldonkey设置!看图(转载)
转自:http://www.nenew.net/ubuntu-mldonkey-application.html 这里不是争论区,amule和mldonkey各有各好,看个人爱好,没有高下,都是程序员 ...