/*AOV Network (activity on Vertex Network): The graph represents the project: which represents the activity with vertices, and arcs represent the constraints between activities. Is the project going smoothly? ----whether there is a forward loop ******************************************* with the method of generating (including all) vertex sequences in the >aov network, the vertex sequence satisfies: In the diagram, if the vertex VI to vertex VJ exists path, In the sequence, Vertex VI is ahead of the vertex VJ. The sequence of vertices that meet the above conditions is called a topological sequence , and the process of producing this sequence is called topological ordering . Does the AOV network contain a forward loop <----> can produce a topological sequence Note: In this test data, for heavy edges, save one The output "NO" is also required for self-loops. */
1#include <iostream>2#include <cstdlib>3#include <cstdio>4#include <cstddef>5#include <iterator>6#include <algorithm>7#include <string>8#include <locale>9#include <cmath>Ten#include <vector> One#include <cstring> A#include <map> -#include <utility> -#include <queue> the#include <stack> -#include <Set> -#include <functional> - using namespacestd; +typedef pair<int,int>PII; -typedefLong LongInt64; + Const intINF =0x3f3f3f3f; A Const intModprime =3046721; at Const DoubleEPS = 1e-9; - Const intMAXN = the; - Const intMAXM =10010; - - intN, M; - BOOLG[MAXN][MAXN]; in intNDCNT[MAXN]; - to + voidSolve () - { the intCNT =0; *stack<int>Stk; $ for(inti =0; I < n; ++i)Panax Notoginseng { - if(0==Ndcnt[i]) the { + Stk.push (i); A } the } + while(!stk.empty ()) - { $ intnode =stk.top (); $ Stk.pop (); -++CNT; - for(inti =0; I < n; ++i) the { - if(G[node][i])Wuyi { the--Ndcnt[i]; - if(0==Ndcnt[i]) Wu { - Stk.push (i); About } $ } - } - } - if(CNT = =N) A { +printf"yes\n"); the } - Else $ { theprintf"no\n"); the } the } the - intMain () in { the #ifdef HOME theFreopen ("inch","R", stdin); About //freopen ("Out", "w", stdout); the #endif the while(~SCANF ("%d%d", &n, &m)) the { + for(inti =0; I < n; ++i) - { theNdcnt[i] =0;Bayi for(intj =0; J < N; ++j) the { theG[I][J] =false; - } - } the the intA, B; the for(inti =0; I < m; ++i) the { -scanf"%d%d", &a, &b); the //How to deal with heavy edges the if(! G[b-1][a-1]) the {94G[b-1][a-1] =true; the++ndcnt[a-1]; the } the }98 Solve (); About } - 101 #ifdef HOME102Cerr <<"Time Elapsed:"<< clock ()/Clocks_per_sec <<"Ms"<<Endl;103 _CrtDumpMemoryLeaks ();104 #endif the return 0;106}
The hdu of topological sequences 5154 Harry and magical computer