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 ...
随机推荐
- java反射-使用反射来操纵方法
一个类的主要成员时方法,辣么我们通过反射获取到一个类的所有方法信息后,总的寻找一种方式去操作调用这些方法,这样反射才有意义有意思. Method对象有一个方法invoke. public O ...
- 携程Apollo(阿波罗)配置中心的Java样例客户端启动和调试
参考:https://github.com/ctripcorp/apollo/wiki/Apollo%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97#23-java%E6%A0 ...
- KLT 光流
一 光流 光流的概念是Gibson在1950年首先提出来的.它是空间运动物体在观察成像平面上的像素运动的瞬时速度,是利用图像序列中像素在时间域上的变化以及相邻帧之间的相关性来找到上一帧跟当前帧之间存在 ...
- scikit-learn:3.3. Model evaluation: quantifying the quality of predictions
參考:http://scikit-learn.org/stable/modules/model_evaluation.html#scoring-parameter 三种方法评估模型的预測质量: Est ...
- Echarts Binning on map 根据真实经纬度渲染数据
要渲染的数据:[经度,维度,值] 例如: var data = [[116.420691626, 39.4574061868, 63],[116.423620497, 39.4574061868, 2 ...
- debug 和release 的区别
http://blog.csdn.net/h_wlyfw/article/details/26688677
- struts2开发中一些概念的理解
对象关系映射(orm)中的两个概念 VO 和 PO: 它们都包含一些属性及这些属性的get/set方法 1.VO:是值对象,可以理解为业务对象,存活在业务层,供业务逻辑使用,当前业务逻辑需要一组什么数 ...
- lvm调整分区大小
1 问题 /home分区占用空间比较大,而/var分区比较小,它们位于同一个磁盘上.该系统安装了lvm. 2 减少/home分区空间 2.1 卸载/home umount /home 2.2 检查文件 ...
- shell脚本,根据字符串获取行号的
awk中不能解析shell变量,建议做法是通过-v传递进去: 1 typeline=`cat $typepath | awk -v str="$typetmp" '/str/{pr ...
- c# IP从192.168.1.1转成int类型
找了一些资料,总结如下: 方法1 .net提供的方法转换IP地址 //字符串转换为数字 System.Net.IPAddress ipaddress = System.Net.IPAddress.Pa ...