题目链接: Coprocessor

题意:

  给出n个待处理的事件(0 - n-1),再给出了n个标(0表示只能在主处理器中处理这个事件,1表示只能在副处理器中处理这个事件),处理器每次能处理多个任务。每个事件有关联,如果一个任务要在副处理器上执行,那它所依赖的任务要么已执行完了,要么和它一起在这个副处理器上同时执行。问副处理器最少调用多少次。

题解:

  首先拓扑排序是肯定要的,先处理出入度为0的点,处理的时候先处理所有入度为0且标为0(只能在主处理器上处理)的点,这样保证每处理一次就把能在副处理器上处理的点全都处理了出来。在处理的过程中保存处理出来的入度为0的点,这样就不用暴力找点了。

 #include<bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5+;
int res[MAX_N];
vector<int> vec[MAX_N];
int in[MAX_N];
queue<int> que1,que2;
int main()
{
int N,M,T;
while(cin>>N>>M)
{
memset(res,,sizeof(res));
memset(in,,sizeof(in));
while(!que1.empty()) que1.pop();
while(!que2.empty()) que2.pop();
for(int i=;i<MAX_N;i++) vec[i].clear();
for(int i=;i<N;i++)
{
scanf("%d",&res[i]);
}
for(int i=;i<M;i++)
{
int a,b;
scanf("%d%d",&a,&b);
vec[b].push_back(a);
in[a] ++;
}
for(int i=;i<N;i++)
{
if(in[i] == )
{
if(res[i] == ) que1.push(i);
else if(res[i] == ) que2.push(i);
} }
int ans = ;
int cnt = N;
while(cnt > )
{
while(!que1.empty())
{ int t = que1.front();que1.pop();
cnt --;
for(int i=;i<vec[t].size();i++)
{
in[vec[t][i]]--;
if(in[vec[t][i]] == )
{
if(res[vec[t][i]] == ) que2.push(vec[t][i]);
else que1.push(vec[t][i]);
}
}
}
if(!que2.empty()) ans ++;
while(!que2.empty())
{
int t = que2.front();que2.pop();
cnt --;
for(int i=;i<vec[t].size();i++)
{
in[vec[t][i]]--;
if(in[vec[t][i]] == )
{
if(res[vec[t][i]] == ) que2.push(vec[t][i]);
else que1.push(vec[t][i]);
}
}
}
}
cout<<ans<<endl;
}
return ;
}

Codeforces 909E. Coprocessor (拓扑、模拟)的更多相关文章

  1. CodeForces 909E Coprocessor(无脑拓扑排序)

    You are given a program you want to execute as a set of tasks organized in a dependency graph. The d ...

  2. Codeforces 909E(Coprocessor,双队列维护)

    题意:给出n个待处理的事件(0 ~n-1),再给出了n个标(0表示只能在主处理器中处理这个事件,1表示只能在副处理器中处理这个事件),处理器每次能处理多个任务.每个事件有关联,如果一个任务要在副处理器 ...

  3. CodeForces 909E Coprocessor

    题解. 贪心,拓扑排序. 和拓扑排序一样,先把$flag$为$0$的点能删的都删光,露出来的肯定都是$flag$为$0$的,然后疯狂删$flag$为$0$的,这些会使答案加$1$,反复操作就可以了. ...

  4. Codeforces Round #455 (Div. 2) 909E. Coprocessor

    题 OvO http://codeforces.com/contest/909/problem/E CF455 div2 E CF 909E 解 类似于拓扑排序地进行贪心, 对于 Ei=0 并且入度为 ...

  5. Codeforces 738D. Sea Battle 模拟

    D. Sea Battle time limit per test: 1 second memory limit per test :256 megabytes input: standard inp ...

  6. Codeforces 626A Robot Sequence(模拟)

    A. Robot Sequence time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  7. CodeForces - 589D(暴力+模拟)

    题目链接:http://codeforces.com/problemset/problem/589/D 题目大意:给出n个人行走的开始时刻,开始时间和结束时间,求每个人分别能跟多少人相遇打招呼(每两人 ...

  8. Codeforces 767B. The Queue 模拟题

    B. The Queue time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  9. Codeforces 510C (拓扑排序)

    原题:http://codeforces.com/problemset/problem/510/C C. Fox And Names time limit per test:2 seconds mem ...

随机推荐

  1. UIWindow的windowLevel详解

    UIWindow的windowLevel详解

  2. [翻译] AYVibrantButton

    AYVibrantButton https://github.com/a1anyip/AYVibrantButton AYVibrantButton is a stylish button with ...

  3. 【C#】#103 动态修改App.config配置文件

    对 C/S模式 下的 App.config 配置文件的AppSetting节点,支持配置信息现改现用,并可以持久保存. 一. 先了解一下如何获取 配置信息里面的内容[获取配置信息推荐使用这个] 1.1 ...

  4. centos6.2/6.3/6.4+nginx+mysql5.5+php5.3.14

    一.安装所需软件包yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype free ...

  5. PHP MemCached高级缓存配置图文教程

    memcache是一个高性能的分布式的内存对象缓存系统,它能够用来存储各种格式的数据,包括图像.视频.文件以及数据库检索的结果等.     1.Memcache相关介绍 memcache是一个高性能的 ...

  6. Hadoop学习之路(五)Hadoop集群搭建模式和各模式问题

    分布式集群的通用问题 当前的HDFS和YARN都是一主多从的分布式架构,主从节点---管理者和工作者 问题:如果主节点或是管理者宕机了.会出现什么问题? 群龙无首,整个集群不可用.所以在一主多从的架构 ...

  7. openvpn 上外网

    Openvpn-2.2.2 实施记录 最终现象: Linux路由: [root@iZrj961wb7816wke73ql7qZ openvpm]# route -n Kernel IP routing ...

  8. undefined reference to `sqrt'的问题

    主要问题是math.h这个头文件虽然在/lib/include 下有定义,但是该文件内并没有sqrt()的定义.解决的办法是:在编译的时候在后面加上-lm,意思是链接到math函数库. 在gcc下用到 ...

  9. 【OC底层】KVO原理

    KVO的原理是什么?底层是如何实现的? KVO是Key-value observing的缩写. KVO是Objective-C是使用观察者设计模式实现的. Apple使用了isa混写(isa-swiz ...

  10. vue 目录结构解析

    ├── README.md 项目介绍├── index.html 入口页面├── build 构建脚本目录│ ├── webpack.base.conf.js webpack基础配置,开发环境,生产环 ...