传送门:https://codeforces.com/gym/100801

题意:

给你一个DAG图,你最多可以进行k次操作,每次操作可以连一条有向边,问你经过连边操作后最小拓扑序的最大值是多少

题解:

最小拓扑序:与普通拓扑序不同的是,用一个小根堆记录入度为0的点做拓扑排序即可

怎么样使得最小拓扑序最大呢?已知拓扑序是入度小的点在前面,那么,如果我们可以使得大的点的度数尽量小或者是小的点度数尽量大就可以使得拓扑序变大了,由于我们只有加边的操作,那么我们可以将边尽量从大的点往小的点去连边

我们定义小根堆q以便于得到最小拓扑序

为了使得最小拓扑序最大,我们这里有一个贪心的过程

贪心的正确性证明:如果当前操作使得当前字典序最大,那么所有操作后字典序一定最大

我们把已经加边的点不直接输出。而是扔进一个大根堆里。

因为这些点之间并没有前后效应,即,我们出任意一个点都是可以的。

所以我们扔进大根堆里,当不得不出的时候,我们一定从大根堆里,选取编号最大的节点拓扑出去

所以,如果当前没有节点入度为0怎么办?很显然,从大根堆里直接拓扑。

每次取出小根堆中的点时,我们都将小根堆中的点往大根堆里面放,如果小根堆为空时,我们就需要对大根堆中的点进行连边操作了,取出大根堆中的点,进行拓扑排序的正常操作记录答案即可;

代码:

  1. /**
  2. *        ┏┓    ┏┓
  3. *        ┏┛┗━━━━━━━┛┗━━━┓
  4. *        ┃       ┃  
  5. *        ┃   ━    ┃
  6. *        ┃ >   < ┃
  7. *        ┃       ┃
  8. *        ┃... ⌒ ...  ┃
  9. *        ┃       ┃
  10. *        ┗━┓   ┏━┛
  11. *          ┃   ┃ Code is far away from bug with the animal protecting          
  12. *          ┃   ┃ 神兽保佑,代码无bug
  13. *          ┃   ┃           
  14. *          ┃   ┃       
  15. *          ┃   ┃
  16. *          ┃   ┃           
  17. *          ┃   ┗━━━┓
  18. *          ┃       ┣┓
  19. *          ┃       ┏┛
  20. *          ┗┓┓┏━┳┓┏┛
  21. *           ┃┫┫ ┃┫┫
  22. *           ┗┻┛ ┗┻┛
  23. */
  24. // warm heart, wagging tail,and a smile just for you!
  25. //
  26. // _ooOoo_
  27. // o8888888o
  28. // 88" . "88
  29. // (| -_- |)
  30. // O\ = /O
  31. // ____/`---'\____
  32. // .' \| |// `.
  33. // / \||| : |||// \
  34. // / _||||| -:- |||||- \
  35. // | | \\ - /// | |
  36. // | \_| ''\---/'' | |
  37. // \ .-\__ `-` ___/-. /
  38. // ___`. .' /--.--\ `. . __
  39. // ."" '< `.___\_<|>_/___.' >'"".
  40. // | | : `- \`.;`\ _ /`;.`/ - ` : | |
  41. // \ \ `-. \_ __\ /__ _/ .-` / /
  42. // ======`-.____`-.___\_____/___.-`____.-'======
  43. // `=---='
  44. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  45. // 佛祖保佑 永无BUG
  46. #include <set>
  47. #include <map>
  48. #include <deque>
  49. #include <queue>
  50. #include <stack>
  51. #include <cmath>
  52. #include <ctime>
  53. #include <bitset>
  54. #include <cstdio>
  55. #include <string>
  56. #include <vector>
  57. #include <cstdlib>
  58. #include <cstring>
  59. #include <iostream>
  60. #include <algorithm>
  61. using namespace std;
  62. typedef long long LL;
  63. typedef pair<LL, LL> pLL;
  64. typedef pair<LL, int> pLi;
  65. typedef pair<int, LL> pil;;
  66. typedef pair<int, int> pii;
  67. typedef unsigned long long uLL;
  68. #define ls rt<<1
  69. #define rs rt<<1|1
  70. #define lson l,mid,rt<<1
  71. #define rson mid+1,r,rt<<1|1
  72. #define bug printf("*********\n")
  73. #define FIN freopen("input.txt","r",stdin);
  74. #define FON freopen("output.txt","w+",stdout);
  75. #define IO ios::sync_with_stdio(false),cin.tie(0)
  76. #define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
  77. #define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
  78. #define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
  79. const double eps = 1e-8;
  80. const int mod = 1e9 + 7;
  81. const int maxn = 3e5 + 5;
  82. const int INF = 0x3f3f3f3f;
  83. const LL INFLL = 0x3f3f3f3f3f3f3f3f;
  84. struct EDGE {
  85. int v, nxt;
  86. } edge[maxn << 1];
  87. int head[maxn], tot;
  88. void add_edge(int u, int v) {
  89. edge[tot].v = v;
  90. edge[tot].nxt = head[u];
  91. head[u] = tot++;
  92. }
  93. int degree[maxn];
  94. vector<int> G[maxn];
  95. priority_queue<int, vector<int>, greater<int> >q; //小根堆放现在的点
  96. priority_queue<int> p;//大根堆放要连的点
  97. int ans[maxn];
  98. int num, used;
  99. int a[maxn], b[maxn];
  100. void solve(int u) {
  101. //排序排序
  102. ans[++num] = u;
  103. for(int i = 0; i < G[u].size(); i++) if(!--degree[G[u][i]]) q.push(G[u][i]);
  104. if(q.empty()) {
  105. if(!p.empty()) {
  106. int v = p.top(); p.pop();
  107. //大往小连边
  108. G[u].push_back(v);
  109. //记录加的边
  110. a[++used] = u; b[used] = v;
  111. solve(v);
  112. }
  113. }
  114. }
  115. int main() {
  116. #ifndef ONLINE_JUDGE
  117. FIN
  118. #endif
  119. // freopen("graph.in", "r", stdin);
  120. // freopen("graph.out", "w+", stdout);
  121. int n, m, k;
  122. scanf("%d%d%d", &n, &m, &k);
  123. memset(head, -1, sizeof(head));
  124. memset(degree, 0, sizeof(degree));
  125. tot = 0;
  126. for(int i = 1, u, v; i <= m; i++) {
  127. scanf("%d%d", &u, &v);
  128. add_edge(u, v);
  129. G[u].push_back(v);
  130. degree[v]++;
  131. }
  132. for(int i = 1; i <= n; i++) {
  133. if(degree[i] == 0) q.push(i);
  134. }
  135. while(!q.empty()) {
  136. //小根堆不为空或者大根堆不为空并且小根堆堆顶的元素小于大根堆堆顶的元素
  137. if(k && (q.size() > 1 || (!p.empty() && q.top() < p.top()))) {
  138. k--;
  139. int u = q.top();
  140. q.pop();
  141. p.push(u);
  142. //如果入度为0的点用完了,就开始连边
  143. if(q.empty()) {
  144. int v = p.top(); p.pop();
  145. G[ans[num]].push_back(v);
  146. //记录加的边
  147. a[++used] = ans[num]; b[used] = v;
  148. solve(v);
  149. }
  150. } else {
  151. //如果当前点是最后一个入度为0的点,那么加边是没有意义的,直接拓扑
  152. //如果当前这个点是当前最后一个入度为0的点,而且没有点是已经被加边的,那这个点如果加边,我们就是浪费了一条边,不如直接拓扑出
  153. //如果当前这个点是当前最后一个入度为0的点,而且它比当前所有已经被加边的点的编号要大,那加边也是在浪费边,也不如直接拓扑出
  154. int v = q.top(); q.pop();
  155. solve(v);
  156. }
  157. }
  158. for(int i = 1; i <= n; i++) {
  159. printf("%d%c", ans[i], i == n ? '\n' : ' ');
  160. }
  161. printf("%d\n", used);
  162. for(int i = 1; i <= used; i++) {
  163. printf("%d %d\n", a[i], b[i]);
  164. }
  165. return 0;
  166. }

codeforces gym100801 Problem G. Graph的更多相关文章

  1. codeforces gym100801 Problem J. Journey to the “The World’s Start”

    传送门:https://codeforces.com/gym/100801 题意: 小明坐地铁,现在有n-1种类型的地铁卡卖,现在小明需要买一种地铁票,使得他可以在t的时间内到达终点站,地铁票的属性为 ...

  2. Codeforces Gym 100513G G. FacePalm Accounting

    G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...

  3. Codeforces Gym 100637G G. #TheDress 暴力

    G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G ...

  4. Codeforces Gym 100513G G. FacePalm Accounting 暴力

    G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...

  5. Codeforces #550 (Div3) - G.Two Merged Sequences(dp / 贪心)

    Problem  Codeforces #550 (Div3) - G.Two Merged Sequences Time Limit: 2000 mSec Problem Description T ...

  6. Educational Codeforces Round 40 G. Castle Defense (二分+滑动数组+greedy)

    G. Castle Defense time limit per test 1.5 seconds memory limit per test 256 megabytes input standard ...

  7. Educational Codeforces Round 37 G. List Of Integers (二分,容斥定律,数论)

    G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  8. 论文解读《Measuring and Relieving the Over-smoothing Problem for Graph NeuralNetworks from the Topological View》

    论文信息 论文标题:Measuring and Relieving the Over-smoothing Problem for Graph NeuralNetworks from the Topol ...

  9. 实验9:Problem G: 克隆人来了!

    想要输出""的话: cout<<"A person whose name is \""<<name<<" ...

随机推荐

  1. docker+jenkins的实现方式(ps.使用dockerfile的方式)!

    继http://www.cnblogs.com/guilty/p/4747993.html之后. 前两天朋友问的,docker+jenkins整合. 我也没搞过,但是正好最近有空,我也很有兴趣,就搞一 ...

  2. 洛谷P3455 [POI2007]ZAP-Queries (莫比乌斯反演)

    题意:求$\sum_{i=1}^{a}\sum_{j=1}^{b}[gcd(i,j)==d]$(1<=a,b,d<=50000). 很套路的莫比乌斯反演. $\sum_{i=1}^{n}\ ...

  3. windows下多版本python安装与pip安装和pip使用 吐血总结

    https://blog.csdn.net/silence2015/article/details/56483892/ 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附 ...

  4. 巨蟒python全栈开发-第11阶段 ansible_project5

    今日大纲 1.命令展示前端页面实现(下面有个断点) 2.命令下发后端展示

  5. iOS 11 适配UIWebView,页面下移20的问题

    方案1: AppDelegate文件 didFinishLaunchingWithOptions()中添加如下代码 if (@available(iOS 11.0, *)) { [[UIScrollV ...

  6. python环境测试MySQLdb、DBUtil、sqlobject性能

    python环境测试MySQLdb.DBUtil.sqlobject性能 首先介绍下MySQLdb.DBUtil.sqlobject: (1)MySQLdb 是用于Python连接Mysql数据库的接 ...

  7. 唯一索引与非唯一索引区别(UNIQUE INDEX, NON-UNIQUE INDEX)

    索引是我们经常使用的一种数据库搜索优化手段.适当的业务操作场景使用适当的索引方案可以显著的提升系统整体性能和用户体验.在Oracle中,索引有包括很多类型.不同类型的索引适应不同的系统环境和访问场景. ...

  8. 使用sqlyog链接多个主机的数据库

  9. laravel 学习笔记blog后台

    https://github.com/almasaeed2010/adminlte composer require "almasaeed2010/adminlte=~2.0"

  10. poj 3384 Feng Shui (Half Plane Intersection)

    3384 -- Feng Shui 构造半平面交,然后求凸包上最远点对. 这题的题意是给出一个凸多边形区域,要求在其中放置两个半径为r的圆(不能超出凸多边形区域),要求求出两个圆心,使得多边形中没有被 ...