OvO http://codeforces.com/contest/909/problem/E

  CF455 div2 E

  CF 909E

  类似于拓扑排序地进行贪心,

  对于 Ei=0 并且入度为 0 的点,创建一个缓冲队列,把这些点的影响到的点先放到缓冲队列中,然后等一次 coprocessor calls 计算完后统一处理,保证每次找 coprocessor calls 的集合的时候,缓冲队列为空

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <queue>
  7.  
  8. using namespace std;
  9.  
  10. const int N=1e5+44;
  11.  
  12. struct node{
  13. int u,v;
  14. int next;
  15. }edge[2*N];
  16.  
  17. int head[N],num,tag[N];
  18. int n,m;
  19. int indg[N];
  20. int que[N],lq,rq;
  21. queue<int> dlt_wait;
  22.  
  23. void addedge(int u,int v)
  24. {
  25. edge[num].u=u;
  26. edge[num].v=v;
  27. edge[num].next=head[u];
  28. head[u]=num++;
  29. }
  30.  
  31. void init()
  32. {
  33. num=0;
  34. memset(head,-1,sizeof(head));
  35. }
  36.  
  37. void dlt(int id)
  38. {
  39. for(int i=head[id];i!=-1;i=edge[i].next)
  40. dlt_wait.push(edge[i].v);
  41. }
  42.  
  43. void clean()
  44. {
  45. int now;
  46. while(!dlt_wait.empty())
  47. {
  48. now=dlt_wait.front(),dlt_wait.pop();
  49. if(--indg[now]==0)
  50. {
  51. if(tag[now])
  52. que[++rq]=now;
  53. else
  54. dlt(now);
  55. }
  56. }
  57. }
  58.  
  59. void solve()
  60. {
  61. int tmp,now;
  62. int ans=0;
  63. lq=1,rq=0;
  64. while(!dlt_wait.empty())
  65. dlt_wait.pop();
  66. for(int i=0;i<n;i++)
  67. if(indg[i]==0)
  68. {
  69. if(tag[i])
  70. que[++rq]=i;
  71. else
  72. dlt(i);
  73. }
  74. clean();
  75. while(lq<=rq)
  76. {
  77. ans++;
  78. while(lq<=rq)
  79. {
  80. now=que[lq++];
  81. for(int i=head[now];i!=-1;i=edge[i].next)
  82. if(--indg[edge[i].v]==0)
  83. {
  84. if(tag[edge[i].v])
  85. que[++rq]=edge[i].v;
  86. else dlt(edge[i].v);
  87. }
  88. }
  89. clean();
  90. }
  91. printf("%d\n",ans);
  92. }
  93.  
  94. int main()
  95. {
  96. int a,b;
  97. memset(indg,0,sizeof(indg));
  98. init();
  99. scanf("%d%d",&n,&m);
  100. for(int i=0;i<n;i++)
  101. scanf("%d",&tag[i]);
  102. for(int i=0;i<m;i++)
  103. {
  104. scanf("%d%d",&a,&b);
  105. addedge(b,a);
  106. indg[a]++;
  107. }
  108. solve();
  109. return 0;
  110. }

  

Codeforces Round #455 (Div. 2) 909E. Coprocessor的更多相关文章

  1. Codeforces Round #455 (Div. 2)

    Codeforces Round #455 (Div. 2) A. Generate Login 题目描述:给出两个字符串,分别取字符串的某个前缀,使得两个前缀连起来的字符串的字典序在所有方案中最小, ...

  2. Codeforces Round #455 (Div. 2) 909D. Colorful Points

    题 OvO http://codeforces.com/contest/909/problem/D CF 455 div2 D CF 909D 解 算出模拟的复杂度之后就是一个很水的模拟题 把字符串按 ...

  3. Codeforces Round #455 (Div. 2) A. Generate Login【贪心】

    A. Generate Login time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. 【Codeforces Round #455 (Div. 2) A】Generate Login

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举两个串的前缀长度就好. 组出来. 排序. 取字典序最小的那个. [代码] #include <bits/stdc++.h& ...

  5. 【Codeforces Round #455 (Div. 2) B】Segments

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理出所有的线 其实就是区间. 总共有n*(n+1)/2个 然后按照左端点.右端点排序 每次取最左边的线. 多种可能就取右端点尽量小 ...

  6. 【Codeforces Round #455 (Div. 2) C】 Python Indentation

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 一个for循环之后. 下一个写代码的地方一是从(x+1,y+1)开始的 然后如果写完了一个simple statement 下次就有 ...

  7. Codeforces Round #455 (Div. 2) D题(花了一个早自习补了昨晚的一道模拟QAQ)

    D. Colorful Points You are given a set of points on a straight line. Each point has a color assigned ...

  8. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  9. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

随机推荐

  1. yum源 epel源 替换及安装

    #!/bin/sh # 备份yum源 zip -r /etc/yum.repos.d/yum_resource_back_up.zip /etc/yum.repos.d/* # 替换yum源 wget ...

  2. Oracle 10g 归档日志满了的解决办法

    如果Oracle的归档日志满了,应用连接数据库就会出错,这时需要手工删除过期的归档日志,方法如下: 1.指定数据库实例 $ export ORACLE_SID=db1 2.进入rman $ rman ...

  3. - RabbitMQ - 0 - 介绍、linux 和windows安装

    目录 一. 介绍 二.windows安装erlang和rabbitMQ 三.Linux安装erlang和RabbitMQ 一. 介绍 rabbitMQ 是基于 erlang 语言开发的, 为了使用 r ...

  4. Redis 原子操作——事务

    MULTI 标记一个事务块的开始. 事务块内的多条命令会按照先后顺序被放进一个队列当中,最后由 EXEC 命令原子性(atomic)地执行. 可用版本: >= 1.2.0 时间复杂度: O(1) ...

  5. Spring中@Component与@Bean的区别

    @Component和@Bean的目的是一样的,都是注册bean到Spring容器中. @Component  VS  @Bean @Component 和 它的子类型(@Controller, @S ...

  6. C++多线程基础学习笔记(十)

    一.Windows临界区的基本用法 CRITICAL_SECTION my_winsc;              //定义一个Windows的临界区,相当于一个mutex变量 InitializeC ...

  7. 网络流Dinic--模板

    #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <cstdio>//sprintf islower isupp ...

  8. opencv实现人脸识别(四) 人脸识别模块

    到这一步就是进行人脸识别了. 流程图: 代码: import cv2 def recognize(cam): recognizer = cv2.face.LBPHFaceRecognizer_crea ...

  9. 12.如何设置ulimit

    ulimit -a用来显示当前的各种用户进程限制 修改所有 linux 用户的环境变量文件:vi /etc/profileulimit -u 10000              #用户的最大进程数u ...

  10. shell习题第13题:监控nginx进程

    [题目要求] 在服务器上写一个脚本,要求如下 1. 每隔10秒去检查而一次服务器上的nginx进程数,如果>=500的时候,就需要自动重启一下nginx服务,并检测启动是否成功 2. 如没有正常 ...