Codeforces Round #455 (Div. 2) 909E. Coprocessor
题
OvO http://codeforces.com/contest/909/problem/E
CF455 div2 E
CF 909E
解
类似于拓扑排序地进行贪心,
对于 Ei=0 并且入度为 0 的点,创建一个缓冲队列,把这些点的影响到的点先放到缓冲队列中,然后等一次 coprocessor calls 计算完后统一处理,保证每次找 coprocessor calls 的集合的时候,缓冲队列为空
- #include <iostream>
- #include <cstring>
- #include <cstdio>
- #include <cmath>
- #include <algorithm>
- #include <queue>
- using namespace std;
- const int N=1e5+44;
- struct node{
- int u,v;
- int next;
- }edge[2*N];
- int head[N],num,tag[N];
- int n,m;
- int indg[N];
- int que[N],lq,rq;
- queue<int> dlt_wait;
- void addedge(int u,int v)
- {
- edge[num].u=u;
- edge[num].v=v;
- edge[num].next=head[u];
- head[u]=num++;
- }
- void init()
- {
- num=0;
- memset(head,-1,sizeof(head));
- }
- void dlt(int id)
- {
- for(int i=head[id];i!=-1;i=edge[i].next)
- dlt_wait.push(edge[i].v);
- }
- void clean()
- {
- int now;
- while(!dlt_wait.empty())
- {
- now=dlt_wait.front(),dlt_wait.pop();
- if(--indg[now]==0)
- {
- if(tag[now])
- que[++rq]=now;
- else
- dlt(now);
- }
- }
- }
- void solve()
- {
- int tmp,now;
- int ans=0;
- lq=1,rq=0;
- while(!dlt_wait.empty())
- dlt_wait.pop();
- for(int i=0;i<n;i++)
- if(indg[i]==0)
- {
- if(tag[i])
- que[++rq]=i;
- else
- dlt(i);
- }
- clean();
- while(lq<=rq)
- {
- ans++;
- while(lq<=rq)
- {
- now=que[lq++];
- for(int i=head[now];i!=-1;i=edge[i].next)
- if(--indg[edge[i].v]==0)
- {
- if(tag[edge[i].v])
- que[++rq]=edge[i].v;
- else dlt(edge[i].v);
- }
- }
- clean();
- }
- printf("%d\n",ans);
- }
- int main()
- {
- int a,b;
- memset(indg,0,sizeof(indg));
- init();
- scanf("%d%d",&n,&m);
- for(int i=0;i<n;i++)
- scanf("%d",&tag[i]);
- for(int i=0;i<m;i++)
- {
- scanf("%d%d",&a,&b);
- addedge(b,a);
- indg[a]++;
- }
- solve();
- return 0;
- }
Codeforces Round #455 (Div. 2) 909E. Coprocessor的更多相关文章
- Codeforces Round #455 (Div. 2)
Codeforces Round #455 (Div. 2) A. Generate Login 题目描述:给出两个字符串,分别取字符串的某个前缀,使得两个前缀连起来的字符串的字典序在所有方案中最小, ...
- Codeforces Round #455 (Div. 2) 909D. Colorful Points
题 OvO http://codeforces.com/contest/909/problem/D CF 455 div2 D CF 909D 解 算出模拟的复杂度之后就是一个很水的模拟题 把字符串按 ...
- 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 ...
- 【Codeforces Round #455 (Div. 2) A】Generate Login
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举两个串的前缀长度就好. 组出来. 排序. 取字典序最小的那个. [代码] #include <bits/stdc++.h& ...
- 【Codeforces Round #455 (Div. 2) B】Segments
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理出所有的线 其实就是区间. 总共有n*(n+1)/2个 然后按照左端点.右端点排序 每次取最左边的线. 多种可能就取右端点尽量小 ...
- 【Codeforces Round #455 (Div. 2) C】 Python Indentation
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 一个for循环之后. 下一个写代码的地方一是从(x+1,y+1)开始的 然后如果写完了一个simple statement 下次就有 ...
- 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 ...
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
随机推荐
- yum源 epel源 替换及安装
#!/bin/sh # 备份yum源 zip -r /etc/yum.repos.d/yum_resource_back_up.zip /etc/yum.repos.d/* # 替换yum源 wget ...
- Oracle 10g 归档日志满了的解决办法
如果Oracle的归档日志满了,应用连接数据库就会出错,这时需要手工删除过期的归档日志,方法如下: 1.指定数据库实例 $ export ORACLE_SID=db1 2.进入rman $ rman ...
- - RabbitMQ - 0 - 介绍、linux 和windows安装
目录 一. 介绍 二.windows安装erlang和rabbitMQ 三.Linux安装erlang和RabbitMQ 一. 介绍 rabbitMQ 是基于 erlang 语言开发的, 为了使用 r ...
- Redis 原子操作——事务
MULTI 标记一个事务块的开始. 事务块内的多条命令会按照先后顺序被放进一个队列当中,最后由 EXEC 命令原子性(atomic)地执行. 可用版本: >= 1.2.0 时间复杂度: O(1) ...
- Spring中@Component与@Bean的区别
@Component和@Bean的目的是一样的,都是注册bean到Spring容器中. @Component VS @Bean @Component 和 它的子类型(@Controller, @S ...
- C++多线程基础学习笔记(十)
一.Windows临界区的基本用法 CRITICAL_SECTION my_winsc; //定义一个Windows的临界区,相当于一个mutex变量 InitializeC ...
- 网络流Dinic--模板
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <cstdio>//sprintf islower isupp ...
- opencv实现人脸识别(四) 人脸识别模块
到这一步就是进行人脸识别了. 流程图: 代码: import cv2 def recognize(cam): recognizer = cv2.face.LBPHFaceRecognizer_crea ...
- 12.如何设置ulimit
ulimit -a用来显示当前的各种用户进程限制 修改所有 linux 用户的环境变量文件:vi /etc/profileulimit -u 10000 #用户的最大进程数u ...
- shell习题第13题:监控nginx进程
[题目要求] 在服务器上写一个脚本,要求如下 1. 每隔10秒去检查而一次服务器上的nginx进程数,如果>=500的时候,就需要自动重启一下nginx服务,并检测启动是否成功 2. 如没有正常 ...