Http://codeforces.com/problemset/problem/909/E
Because of the two queue, the push can be operated uniformly without affecting each other. Two queue equals equal, just q[1] added count.
Although it was a topological order at first, some of the details were wasted time in tle.
For example, I used a vis array to judge the termination state, in fact, just judge the number of pop points is good
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cstdlib>6#include <cmath>7#include <vector>8#include <queue>9 #defineIO Ios::sync_with_stdio (false); Cin.tie (0);Ten Const intmod=1e9+7; Onetypedefintll; A using namespacestd; -vector<int> vec[100010]; - intN, M, indegree[100010], cnt=0, num=0, vis[100010]; the inta[100010], x, Y, tail=-1; -queue<int> q[2]; - voidtopo () - { + for(inti =0; I < n; i++){ - if(!Indegree[i]) { + Q[a[i]].push (i); A } at } - while(num<N) { - if(!q[0].empty ()) - while(!q[0].empty ()) { - intt = q[0].front (); -q[0].pop (); innum++; -Tail=T; to for(inti =0; I < vec[t].size (); i++){ +indegree[vec[t][i]]--; - if(!Indegree[vec[t][i]]) { the Q[a[vec[t][i]]].push (Vec[t][i]); * } $ }Panax Notoginseng } - if(!q[1].empty ()) { thecnt++; + while(!q[1].empty ()) { A intt = q[1].front (); theq[1].pop (); +num++; -Tail=T; $ for(inti =0; I < vec[t].size (); i++){ $indegree[vec[t][i]]--; - if(!Indegree[vec[t][i]]) { - Q[a[vec[t][i]]].push (Vec[t][i]); the } - }Wuyi } the } - } Wu } - intMain () About { $ IO; -memset (Vis,0,sizeof(Vis)); -CIN >> N >>m; - for(inti =0; I < n; i++){ ACIN >>A[i]; + } the for(inti =0; I < m; i++){ -Cin >> x >> y;//x<-y $ vec[y].push_back (x); theindegree[x]++; the } the topo (); thecout << CNT <<Endl; - return 0; in}
codefoces909e coprocessor (topological sort)