题目链接: 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. ORA-28547:(Navicat Premium连接oracle报错)

    1.背景 Navicat Premium连接oracle报 ORA-28547:connection to server failed, probable Oracle Net admin errro ...

  2. Python学习---模拟微信网页登录180410

    WEB微信 网页登录的猜想: a. 访问页面出现二维码 b. 长轮询监听是否已经扫码并且点击确认 c. 如何进行会话保持 d. 如何获取用户列表 e. 如何发送消息(接收消息) 过程:访问微信官网[h ...

  3. 将亚马逊aws的ec2服务器的登陆方式改为密码登陆

    1.在用密钥登陆ec2后,为root用户创建密码: sudo passwd root 系统会让你输入两次密码 2.切换为root用户,并且编辑sshd_config文件,PasswordAuthent ...

  4. Hello World ! 第一篇随笔

    Hello World ! 第一篇随笔 /* * Language: C++ * Code Name: Hello World ! * @author Metak */ #include <io ...

  5. 026.3 网络编程 TCP聊天

    分为客户端和服务端,分别进行收发操作##########################################################################客户端:###思 ...

  6. HBase的常用Java API

    1. 创建HBase表的对象 HBase表的对项名字叫HTable,创建它的方法有很多,常见的有如下: org.apache.hadoop.hbase.client.HTable hTable = n ...

  7. ubuntu16.04安装visual-studio-code

    微软主页的安装说明,https://code.visualstudio.com/docs/setup/linux    有一点英语基础就能看懂,写的很好,一切以官方文档为主 方法一:可以使用umake ...

  8. 切换composer国内镜像

    composer config -g repo.packagist composer https://packagist.phpcomposer.com

  9. 如何使用jackson美化输出json/xml

    如何使用jackson美化输出json/xml 1.美化POJO序列化xml 下面将POJO列化为xml并打印. Person person = new Person(); //设置person属性 ...

  10. shell基础--字符串和变量的操作

    一.统计字符串长度 1.wc –L [root@~_~day4]# echo "hello" | wc -L 5 2.expr length string [root@~_~day ...