CodeForces 909E Coprocessor(无脑拓扑排序)
Some of the tasks in the graph can only be executed on a coprocessor, and the rest can only be executed on the main processor. In one coprocessor call you can send it a set of tasks which can only be executed on it. For each task of the set, all tasks on which it depends must be either already completed or be included in the set. The main processor starts the program execution and gets the results of tasks executed on the coprocessor automatically.
Find the minimal number of coprocessor calls which are necessary to execute the given program.
The first line contains two space-separated integers N (1 ≤ N ≤ 105) — the total number of tasks given, and M (0 ≤ M ≤ 105) — the total number of dependencies between tasks.
The next line contains N space-separated integers
. If Ei = 0, task i can only be executed on the main processor, otherwise it can only be executed on the coprocessor.
The next M lines describe the dependencies between tasks. Each line contains two space-separated integers T1 and T2 and means that task T1 depends on task T2 (T1 ≠ T2). Tasks are indexed from 0 to N - 1. All M pairs (T1, T2) are distinct. It is guaranteed that there are no circular dependencies between tasks.
Output one line containing an integer — the minimal number of coprocessor calls necessary to execute the program.
4 3
0 1 0 1
0 1
1 2
2 3
2
4 3
1 1 1 0
0 1
0 2
3 0
1
In the first test, tasks 1 and 3 can only be executed on the coprocessor. The dependency graph is linear, so the tasks must be executed in order 3 -> 2 -> 1 -> 0. You have to call coprocessor twice: first you call it for task 3, then you execute task 2 on the main processor, then you call it for for task 1, and finally you execute task 0 on the main processor.
In the second test, tasks 0, 1 and 2 can only be executed on the coprocessor. Tasks 1 and 2 have no dependencies, and task 0 depends on tasks 1 and 2, so all three tasks 0, 1 and 2 can be sent in one coprocessor call. After that task 3 is executed on the main processor.
题意:给你一堆任务,有些要用主处理器处理,有些要用副处理器处理,副处理器可以一次处理很多个任务,一个任务能被执行的条件为前继任务已经被执行过了或者前继任务和自己同时被放进副处理器处理,现在给你这些前继任务的关系和每个任务处理要用的处理器,求副处理器最少运行了几次,保证关系是一张有向无环图
题解:
用贪心的思想,每次把所有当前能做得要用主处理器的任务都做光,那么此时能一锅端的副处理器任务肯定也是最多的。所以首先建两个队列,一个放拓扑排序搜到的要用主处理器做得任务,一个放副处理器做的任务,每次先拓扑排序搜到没有能用主处理器做的任务为止,然后如果副处理器的队列里还有别的数,那么ans++,把副处理器队列里的数继续拿出来进行拓扑排序,直到所有数都被搜过为止,复杂度就是拓扑排序的复杂度,还是很优越的。(恕我直言,这一场简直有毒,ab姑且不讲,c题无脑dp,d题滑稽模拟,e题竟然如此简单……不过f题还是不错的,有d题难度(雾)这不会就是传说中的div3吧2333)
代码如下:
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; struct node
{
int val,du;
} a[]; int n,m,vis[],ans;
vector<int> g[]; int main()
{
scanf("%d %d",&n,&m);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i].val);
}
for(int i=; i<=m; i++)
{
int from,to;
scanf("%d%d",&from,&to);
g[to+].push_back(from+);
a[from+].du++;
}
queue<int> q[];
for(int i=; i<=n; i++)
{
if(!a[i].du)
{
q[a[i].val].push(i);
}
}
while((!q[].empty())||(!q[].empty()))
{
while(!q[].empty())
{
int u=q[].front();
vis[u]=;
q[].pop();
for(int i=; i<g[u].size(); i++)
{
if(!vis[g[u][i]])
{
a[g[u][i]].du--;
if(!a[g[u][i]].du)
{
q[a[g[u][i]].val].push(g[u][i]);
}
}
}
}
if(!q[].empty())
{
ans++;
while(!q[].empty())
{
int u=q[].front();
vis[u]=;
q[].pop();
for(int i=; i<g[u].size(); i++)
{
if(!vis[g[u][i]])
{
a[g[u][i]].du--;
if(!a[g[u][i]].du)
{
q[a[g[u][i]].val].push(g[u][i]);
}
}
}
}
}
}
printf("%d\n",ans);
}
CodeForces 909E Coprocessor(无脑拓扑排序)的更多相关文章
- Codeforces 909E. Coprocessor (拓扑、模拟)
题目链接: Coprocessor 题意: 给出n个待处理的事件(0 - n-1),再给出了n个标(0表示只能在主处理器中处理这个事件,1表示只能在副处理器中处理这个事件),处理器每次能处理多个任务. ...
- codeforces 915D Almost Acyclic Graph 拓扑排序
大意:给出一个有向图,问能否在只去掉一条边的情况下破掉所有的环 解析:最直接的是枚举每个边,将其禁用,然后在图中找环,如果可以就YES,都不行就NO 复杂度O(N*M)看起来不超时 但是实现了以后发现 ...
- Codeforces 909E(Coprocessor,双队列维护)
题意:给出n个待处理的事件(0 ~n-1),再给出了n个标(0表示只能在主处理器中处理这个事件,1表示只能在副处理器中处理这个事件),处理器每次能处理多个任务.每个事件有关联,如果一个任务要在副处理器 ...
- CodeForces 510C Fox And Names (拓扑排序)
<题目链接> 题目大意: 给你一些只由小写字母组成的字符串,现在按一定顺序给出这些字符串,问你怎样从重排字典序,使得这些字符串按字典序排序后的顺序如题目所给的顺序相同. 解题分析:本题想到 ...
- Codeforces Round #290 (Div. 2) 拓扑排序
C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- CodeForces 909E Coprocessor
题解. 贪心,拓扑排序. 和拓扑排序一样,先把$flag$为$0$的点能删的都删光,露出来的肯定都是$flag$为$0$的,然后疯狂删$flag$为$0$的,这些会使答案加$1$,反复操作就可以了. ...
- Codeforces 919D:Substring(拓扑排序+DP)
D. Substring time limit: per test3 seconds memory limit: per test256 megabytes inputstandard: input ...
- Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 拓扑排序
C. Mail Stamps One day Bob got a letter in an envelope. Bob knows that when Berland's post offic ...
- Codeforces 875C National Property(拓扑排序)
题目链接 National Property 给定n个单词,字符集为m 现在我们可以把其中某些字母变成大写的.大写字母字典序大于小写字母. 问是否存在一种方案使得给定的n个单词字典序不下降. 首先判 ...
随机推荐
- Redis进阶实践之十九 Redis如何使用lua脚本
一.引言 redis学了一段时间了,基本的东西都没问题了.从今天开始讲写一些redis和lua脚本的相关的东西,lua这个脚本是一个好东西,可以运行在任何平台上,也可以嵌入 ...
- mysql在线修复主从同步
要实现MySQL的主从复制,首先必须打开Master端的binlog记录功能,否则就无法实现.因为整个复制过程实际上就是Slave从aster端获取binlog日志,然后再在Slave上以相同顺序执行 ...
- 详细分析du和df的统计结果为什么不一样
今天有个人问我du和df的统计结果为什么会不同.给他解析了一番,后来想想还是写篇文章从原理上来分析分析. 我们常常使用du和df来获取目录或文件系统已占用空间的情况.但它们的统计结果是不一致的,大多数 ...
- 网络通信 --> CRC校验
CRC校验 一.什么是CRC校验 循环校验码(Jyclic Redundancy Check,简称CRC码): 是数据通信领域中最常用的一种差错校验码,其特征是信息字段和校验字段的长度可以任意 ...
- JAVA连接SAP
1.首先需要在SAP事务码SE37中新建一个可以被远程调用的RFC 事务码:SE37 新建一个函数组:输入事务码SE37回车后,来到函数构建器屏幕,到上面一排菜单栏:转到 -> 函数组 -> ...
- C语言——第三次作业
题目1.A乘以B 1.实验代码 #include <stdio.h> int main() { int A,B,C; scanf("%d %d",&A,& ...
- 200行Python代码实现2048
200行Python代码实现2048 一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环境,实验中会用到桌面 ...
- Flask 学习 七 用户认证
使用werkzeug 实现密码散列 from werkzeug.security import generate_password_hash,check_password_hash class Use ...
- JS中的 map, filter, some, every, forEach, for...in, for...of 用法总结
1.map 有返回值,返回一个新的数组,每个元素为调用func的结果. let list = [1, 2, 3, 4, 5]; let other = list.map((d, i) => { ...
- nyoj 移位密码
移位密码 时间限制:1000 ms | 内存限制:65535 KB 难度:0 描述 移位密码是最简单的一类代替密码,具体算法就是将字母表的字母右移k个位置(k<26),并对字母表长度作模 ...