Codeforces 909E coprocessor (no brain topological sorting)

Source: Internet
Author: User

You is given a program, want to execute as a set of the 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 is no directed circular dependencies between tasks. A task can is executed if all tasks it depends on has already completed.

Some of the the tasks in the graph can is only is executed on a coprocessor, and the rest can is only is executed on the main proces Sor. In the one coprocessor call can send it a set of the tasks which can only is executed on it. For each task of the set, all tasks on which it depends must is either already completed or be included in the set. The main processor starts the program execution and gets the results of the tasks executed on the coprocessor automatically.

Find the minimal number of coprocessor calls which is necessary to execute the given program.

Input

The first line contains, space-separated integers n (1≤ n ≤105)-the total number of T asks 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 is executed on the main processor, otherwise it Can only is executed on the coprocessor.

The nextM lines describe the dependencies between tasks. Each line contains space-separated integers  T1 and T2 and means that task t1 depends on task T 2 (t1≠ t2). Tasks is indexed from 0 to N -1. All M pairs (t1, t2) is distinct. It is guaranteed this there is no circular dependencies between tasks.

Output

Output one line containing a integer-the minimal number of coprocessor calls necessary to execute the program.

Examples input Copy
4 3
0 1 0 1
0 1
1 2
2 3
Output Copy
2
input Copy
4 3
1 1 1 0
0 1
0 2
3 0
Output Copy
1
Note

In the first Test, Tasks 1 and 3 can is executed on the coprocessor. The dependency graph is linear, so the tasks must are executed in order 3, 2, 1 and 0. Coprocessor Twice:first you call it for Task 3 and then you execute Task 2 on the main processor. Call it for Task 1, and finally you execute task 0 on the main processor.

In the second Test, tasks 0, 1, and 2 can is executed on the coprocessor. Tasks 1 and 2 has no dependencies, and task 0 depends on tasks 1 and 2, so all three tasks 0, 1 and 2 can is sent in one Coprocessor call. After, Task 3 is executed on the main processor.

Test instructions: give you a bunch of tasks, some to use the main processor processing, some to be processed by the secondary processor, the secondary processor can handle a number of tasks at a time, a task can be carried out by the pre-task has been executed or the previous task and itself at the same time being put into the secondary processor processing, Now give you these pre-mission relations and each task processing to use the processor, the secondary processor at least a few times to run, to ensure that the relationship is a direction-free graph

Exercises

With greedy thoughts, every time you can do all the tasks that are currently done to use the main processor to do light, then at this time can Yiguoduan of the sub-processor task is certainly the most. So first build two queues, a topological sorting search to use the main processor to do the task, a sub-processor to do the task, each time the topological sort search to not be able to use the main processor to do the task, and then if the secondary processor queue There are other numbers, then ans++, Keep the number of sub-processors in the queue for topological sequencing until all the numbers have been searched, and complexity is the complexity of topological sequencing, or it is superior. (with all due respect, this one is simply poisonous, AB does not speak, c no brain dp,d problem funny simulation, e question is so simple ... But the F question is still good, has the difficulty of D question (fog) This is not the legendary Div3 bar 2333)

The code is as follows:

#include <queue>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;structnode{intVal,du;} a[100010];intn,m,vis[100010],ans;vector<int> g[100010];intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<=n; i++) {scanf ("%d",&a[i].val); }     for(intI=1; i<=m; i++)    {        int  from, to; scanf ("%d%d",& from,&to ); G[to+1].push_back ( from+1); a[ from+1].du++; } Queue<int> q[2];  for(intI=1; i<=n; i++)    {        if(!A[i].du)        {Q[a[i].val].push (i); }    }     while((!q[0].empty ()) | | (!q[1].empty ())) { while(!q[0].empty ()) {intu=q[0].front (); Vis[u]=1; q[0].pop ();  for(intI=0; 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[1].empty ()) {ans++;  while(!q[1].empty ()) {intu=q[1].front (); Vis[u]=1; q[1].pop ();  for(intI=0; 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 (no brain topological sorting)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.