思路:

拓扑排序。贪心。

实现:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. vector<int> G[];
  4. int n1, n2;
  5. inline void help(queue<int>& a, queue<int>& b, int x)
  6. {
  7. if (x > n1) b.push(x);
  8. else a.push(x);
  9. }
  10. bool same(int x, int y)
  11. {
  12. if (x >= && x <= n1 && y >= && y <= n1) return true;
  13. if (x > n1 && y > n1) return true;
  14. return false;
  15. }
  16. int cal(queue<int> a, queue<int> b, vector<int>in, bool set)
  17. {
  18. int cnt = , last = -, rnd = ;
  19. while (!a.empty() || !b.empty())
  20. {
  21. if ((!set && rnd) || set)
  22. {
  23. while (!a.empty() && !in[a.front()])
  24. {
  25. int tmp = a.front(); a.pop();
  26. if (!same(tmp, last)) cnt++;
  27. last = tmp;
  28. for (int i = ; i < (int)G[tmp].size(); i++)
  29. {
  30. int son = G[tmp][i];
  31. if (--in[son] == ) help(a, b, son);
  32. }
  33. }
  34. }
  35. while (!b.empty() && !in[b.front()])
  36. {
  37. int tmp = b.front(); b.pop();
  38. if (!same(tmp, last)) cnt++;
  39. last = tmp;
  40. for (int i = ; i < (int)G[tmp].size(); i++)
  41. {
  42. int son = G[tmp][i];
  43. if (--in[son] == ) help(a, b, son);
  44. }
  45. }
  46. rnd++;
  47. }
  48. return cnt;
  49. }
  50. int main()
  51. {
  52. int d, x, y;
  53. while (~scanf("%d %d %d", &n1, &n2, &d), n1 + n2 + d)
  54. {
  55. for (int i = ; i <= n1 + n2; i++) G[i].clear();
  56. vector<int> in(n1 + n2 + , );
  57. while (d--)
  58. {
  59. scanf("%d %d", &x, &y);
  60. G[y].push_back(x);
  61. in[x]++;
  62. }
  63. queue<int> a, b;
  64. for (int i = ; i <= n1 + n2; i++)
  65. {
  66. if (!in[i]) help(a, b, i);
  67. }
  68. printf("%d\n", min(cal(a, b, in, true), cal(a, b, in, false)) + );
  69. }
  70. return ;
  71. }

poj1778 All Discs Considered的更多相关文章

  1. POJ 1778 All Discs Considered(拓扑排序)

    点我看题目 题意 :其实题意我也说不清楚,因为比赛的时候我盯着看了1个小时也没看懂....就是两个磁盘,第一个有n1的安装包,编号为1~n1,第二个有n2个安装包,编号为n1~n2.给你d对关系,(x ...

  2. All Discs Considered(拓扑排序)

    http://poj.org/problem?id=1778 题意:有两个DVD,第一个DVD上有编号为1~n1的安装包,第二个DVD上有编号为n1+1~n1+n2的安装包,给出m组关系(a,b) 表 ...

  3. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  4. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  5. 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 ...

  6. [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 ...

  7. Exception Handling Considered Harmful

    异常处理被认为存在缺陷 Do, or do not. There is no try. - Yoda, The Empire Strikes Back (George Lucas) by Jason ...

  8. Which ports are considered unsafe on Chrome

    1, // tcpmux 7, // echo 9, // discard 11, // systat 13, // daytime 15, // netstat 17, // qotd 19, // ...

  9. 最小圆覆盖(Smallest Enclosing Discs)

    随机增量算法(a randomized incremental algorithm) #define sqr(x) ((x)*(x)) #define EPS 1e-4 struct P{ doubl ...

随机推荐

  1. operamasks—omMessageTip的使用

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  2. test markdown 写博客

    欢迎使用 Cmd Markdown 编辑阅读器 我们理解您需要更便捷更高效的工具记录思想,整理笔记.知识,并将其中承载的价值传播给他人,Cmd Markdown 是我们给出的答案 -- 我们为记录思想 ...

  3. kafka-manager 的编译和使用(附安装包)

    kafka-manager 的编译和使用(附安装包) 学习了:https://my.oschina.net/wangjiankui/blog/653139

  4. IOS开发UI篇--一个支持图文混排的ActionSheet

    一.简单介绍 UIActionSheet是IOS提供给我们开发人员的底部弹出菜单控件.一般用于菜单选择.操作确认.删除确认等功能.IOS官方提供的下面方式对UIActionView进行实例化: - ( ...

  5. poj 2728 最优比例生成树(01分数规划)模板

    /* 迭代法 :204Ms */ #include<stdio.h> #include<string.h> #include<math.h> #define N 1 ...

  6. myEclipse怎样将程序部署到tomcat(附录MyEclipse调试快捷键)

    部署 1.选中你要部署的项目,在工具栏找到 Deploy MyEclipse J2EE Project to Server 2.单击Add,即出现例如以下界面.选择对应的Server,要和你在配置to ...

  7. 理解C语言中指针的声明以及复杂声明的语法

    昨天刚把<C程序设计语言>中"指针与数组"章节读完,最终把心中的疑惑彻底解开了.如今记录下我对指针声明的理解.顺便说下怎样在C语言中创建复杂声明以及读懂复杂声明. 本文 ...

  8. Node后台使用mysql并开启事务

    如题:node后台使用mysql数据库,并使用事务来管理数据库操作. 这里主要讲一个事务的封装并写了一个INSERT 插入操作. code: 基础code: db.config.js const my ...

  9. 玩转单元測试之WireMock -- Web服务模拟器

    WireMock 是一个灵活的库用于 Web 服务測试,和其它測试工具不同的是.WireMock 创建一个实际的 HTTPserver来执行你的 Web 服务以方便測试. 它支持 HTTP 响应存根. ...

  10. 编译自己的gcc

    1 编译gcc需要的依赖 gmp mpfr mpc isl binutils 将它们都安装在同一个目录下即可. 2 --disable-nls 将native language support关掉,只 ...