A11539 | Coprocessor
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a program you want to execute as a set of tasks organized in a dependency graph. The dependency graph is a directed acyclic graph: each task can depend on results of one or several other tasks, and there are no directed circular dependencies between tasks. A task can only be executed if all tasks it depends on have already completed.
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.
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<=10^{5}$ ) — the total number of tasks given, and $M$ ( $0<=M<=10^{5}$ ) — the total number of dependencies between tasks.
The next line contains $N$ space-separated integers . If $E_{i}=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 $T_{1}$ and $T_{2}$ and means that task $T_{1}$ depends on task $T_{2}$ ( $T_{1}≠T_{2}$ ). Tasks are indexed from $0$ to $N-1$ . All $M$ pairs $(T_{1},T_{2})$ are distinct. It is guaranteed that there are no circular dependencies between tasks.
The next line contains $N$ space-separated integers . If $E_{i}=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 $T_{1}$ and $T_{2}$ and means that task $T_{1}$ depends on task $T_{2}$ ( $T_{1}≠T_{2}$ ). Tasks are indexed from $0$ to $N-1$ . All $M$ pairs $(T_{1},T_{2})$ 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.
输入输出样例
输入 #1
4 3 0 1 0 1 0 1 1 2 2 3
输出 #1
2
输入 #2
4 3 1 1 1 0 0 1 0 2 3 0
输出 #2
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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted