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 GNOME下解决Sublime Text3中文输入(ibus)(转)
解决方法: 1.进入Sublime Text3插件管理文件夹 cd ~/.config/sublime-text-3/Packages 2.获取InputHelper插件 git clone http ...
- http转https
1.先用jdk生成证书 先跳转到jdk的bin目录下:E:\Program Files\Java\jdk1.8.0_91\bin>keytool -genkey -alias tomcat -k ...
- 功能超级强大的网络工具nc
摘自:http://www.linuxso.com/command/nc.html 功能说明:功能强大的网络工具语 法:nc [-hlnruz][-g<网关...>][-G<指向器数 ...
- 传智播客ADO.Net项目开发教程具体解释
内容简单介绍: 本教程为传智播客.Net培训课堂的现场录像,请到140623ls" target="_blank">传智播客.Net学院下载很多其它免费.Net视频 ...
- MariaDB ----增删改
1> 查看表的(创建)状态: show careate table students1; 注: 自增, (保护机制) 好马不吃回头草 2>查看 字段:( 查看表的结构) des ...
- C中多线程开发
1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期.solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,可是在一个进程(proce ...
- WEB端应该使用DataTable/DataSet吗?
有一次和同事讨论起具体的技术细节,同事说不要用什么实体类,从数据库访问到的数据,直接用DataTable.DataSet 就好.理由是,从获取到的数据集转换成实体类,有一定的性能损耗. 呵呵,性能.我 ...
- win32gui.EnumWindows my.os.EnumWindows.py
import win32guidef _MyCallback(hwnd, extra): windows = extra temp = [] temp.append(hex(hwnd)) temp.a ...
- Codeforces Round #100 A. New Year Table
A. New Year Table time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- oracle type类型
转载 http://blog.sina.com.cn/s/blog_6cfb6b090100ve92.html 转自网络,具体用法我会再细化 1.概念 方法:是在对象类型说明中用关键字 MEM ...