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 ...
随机推荐
- operamasks—omMessageTip的使用
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- test markdown 写博客
欢迎使用 Cmd Markdown 编辑阅读器 我们理解您需要更便捷更高效的工具记录思想,整理笔记.知识,并将其中承载的价值传播给他人,Cmd Markdown 是我们给出的答案 -- 我们为记录思想 ...
- kafka-manager 的编译和使用(附安装包)
kafka-manager 的编译和使用(附安装包) 学习了:https://my.oschina.net/wangjiankui/blog/653139
- IOS开发UI篇--一个支持图文混排的ActionSheet
一.简单介绍 UIActionSheet是IOS提供给我们开发人员的底部弹出菜单控件.一般用于菜单选择.操作确认.删除确认等功能.IOS官方提供的下面方式对UIActionView进行实例化: - ( ...
- poj 2728 最优比例生成树(01分数规划)模板
/* 迭代法 :204Ms */ #include<stdio.h> #include<string.h> #include<math.h> #define N 1 ...
- myEclipse怎样将程序部署到tomcat(附录MyEclipse调试快捷键)
部署 1.选中你要部署的项目,在工具栏找到 Deploy MyEclipse J2EE Project to Server 2.单击Add,即出现例如以下界面.选择对应的Server,要和你在配置to ...
- 理解C语言中指针的声明以及复杂声明的语法
昨天刚把<C程序设计语言>中"指针与数组"章节读完,最终把心中的疑惑彻底解开了.如今记录下我对指针声明的理解.顺便说下怎样在C语言中创建复杂声明以及读懂复杂声明. 本文 ...
- Node后台使用mysql并开启事务
如题:node后台使用mysql数据库,并使用事务来管理数据库操作. 这里主要讲一个事务的封装并写了一个INSERT 插入操作. code: 基础code: db.config.js const my ...
- 玩转单元測试之WireMock -- Web服务模拟器
WireMock 是一个灵活的库用于 Web 服务測试,和其它測试工具不同的是.WireMock 创建一个实际的 HTTPserver来执行你的 Web 服务以方便測试. 它支持 HTTP 响应存根. ...
- 编译自己的gcc
1 编译gcc需要的依赖 gmp mpfr mpc isl binutils 将它们都安装在同一个目录下即可. 2 --disable-nls 将native language support关掉,只 ...