Harry and Magical Computer
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Problem DescriptionIn reward of being yearly outstanding magic student, Harry gets a magical computer. When the computer begins-deal with a process, it'll work until the ending of the processes. One day the computer got N processes to deal with. We number the processes from 1 to n. However there is some dependencies between some processes. When there exists a dependencies (a, b), it means process B must is finished before process a. By knowing all the M dependencies, Harry wants to know if the computer can finish all the n processes.
Inputthere is several test cases, you should process to the end of file.
For each test case, there is numbers N m on the first line, indicates the number processes and the number of Dependen Cies.1≤n≤,1≤m≤10000
The next following m lines, each line contains the numbers a B, indicates a dependencies (a, B).1≤a,b≤n
Outputoutput one line for each test case.
If the computer can finish all the process print "YES" (without quotes).
Else print "NO" (without quotes).
Sample INPUT3 23 12 13 33 22 11 3
Sample Outputyesno
Sourcebestcoder Round #25 Test instructions: There is a direction to the graph; Train of thought: topological Finish no point;
#include <bits/stdc++.h>using namespacestd;#definell Long Long#defineMoD 1000000007#defineESP 0.00000000001Const intn=2e3+Ten, m=1e6+Ten, inf=1e9;intN,m;vector<int>Edge[n];intDu[n];intMain () { while(~SCANF ("%d%d",&n,&m)) {queue<int>Q; Memset (Du,0,sizeof(du)); for(intI=0; i<=n;i++) edge[i].clear (); intans=0; for(intI=1; i<=m;i++) { intu,v; scanf ("%d%d",&u,&v); Edge[u].push_back (v); DU[V]++; } for(intI=1; i<=n;i++) { if(!Du[i]) Q.push (i); } while(!Q.empty ()) { intv=Q.front (); Q.pop (); Ans++; for(intI=0; I<edge[v].size (); i++) {Du[edge[v][i]]--; if(!Du[edge[v][i]] Q.push (edge[v][i]); } } if(ans==N) printf ("yes\n"); Elseprintf ("no\n"); } return 0;}
Hdu 5154 Harry and magical computer topological sort