Topology ordering.
1 /*5154*/2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cstdlib>6#include <queue>7#include <vector>8 using namespacestd;9 Ten #defineMAXN 105 One Avector<int>VEC[MAXN]; -vector<int>:: iterator iter; - intCNT[MAXN]; the intN, M; - - voidinit () { - inti; + -memset (CNT,0,sizeof(CNT)); + for(i=1; i<=n; ++i) A vec[i].clear (); at } - - BOOLTopsort () { - intI, J, K, TMP; - intc =0; -queue<int>Q; in - for(i=1; i<=n; ++i) { to if(Cnt[i] = =0) { + Q.push (i); - } the } * $ while(!Q.empty ()) {Panax Notoginseng++C; -i =Q.front (); the Q.pop (); + for(Iter=vec[i].begin (); Iter!=vec[i].end (); + +ITER) { A if(--cnt[*iter] = =0) { theQ.push (*iter); + } - } $ } $ - if(C <N) - return false; the Else - return true;Wuyi } the - intMain () { Wu intI, J, K; - BOOLFlag; About $ #ifndef Online_judge -Freopen ("data.in","R", stdin); - #endif - A while(SCANF ("%d%d", &n, &m)! =EOF) { + init (); the for(i=0; i<m; ++i) { -scanf"%d%d", &j, &k); $ Vec[j].push_back (k); the++Cnt[k]; the } theFlag =Topsort (); the if(flag) -printf"yes\n"); in Else theprintf"no\n"); the } About the return 0; the}
"Hdoj" 5154 Harry and magical computer