CF909E.Coprocessor

普及+/提高

通过率:0%

时间限制:1.50s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

你被给定一个待执行的程序,该程序由一组任务构成,并以依赖图的形式组织。该依赖图是一个有向无环图(DAG):每个任务可能依赖于一个或多个其他任务的结果,且任务之间不存在有向环形依赖关系。一个任务仅当其所有依赖任务均已执行完成时,方可执行。

图中部分任务只能在协处理器(coprocessor)上执行,其余任务则只能在主处理器(main processor)上执行。一次协处理器调用可向协处理器发送一组仅能在协处理器上执行的任务。对于该组中的每个任务,其所有依赖任务必须要么已经完成,要么也包含在该组内。主处理器启动程序执行,并能自动获取协处理器上执行任务的结果。

请找出执行该程序所需的最少协处理器调用次数。

输入格式

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 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.

第一行包含两个以空格分隔的整数 NN(1≤N≤1051 \leq N \leq 10^5)——任务总数,以及 MM(0≤M≤1050 \leq M \leq 10^5)——任务间依赖关系的总数。

下一行包含 NN 个以空格分隔的整数 。若 Ei=0E_i = 0,则任务 ii 只能在主处理器上执行;否则,它只能在协处理器上执行。

接下来的 MM 行描述任务之间的依赖关系。每行包含两个以空格分隔的整数 T1T_1 和 T2T_2,表示任务 T1T_1 依赖于任务 T2T_2(T1≠T2T_1 \neq T_2)。任务编号从 00 到 N−1N-1。所有 MM 对 (T1,T2)(T_1, T_2) 均互不相同。保证任务之间不存在循环依赖。

输出格式

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.

在第一个测试用例中,任务 1 和任务 3 只能在协处理器上执行。依赖图是线性的,因此任务必须按顺序 3 → 2 → 1 → 0 执行。你需要调用协处理器两次:首先为任务 3 调用协处理器,然后在主处理器上执行任务 2,接着为任务 1 调用协处理器,最后在主处理器上执行任务 0。

在第二个测试用例中,任务 0、1 和 2 只能在协处理器上执行。任务 1 和任务 2 之间没有依赖关系,而任务 0 依赖于任务 1 和任务 2,因此这三个任务 0、1 和 2 可以在一次协处理器调用中全部发送。之后,任务 3 在主处理器上执行。

输入解题思路,AI测评打分。不知道怎么写?

首页