The original question is basically the detection of a series of processes between the existence of a circular dependency problem, such as: A->b->c->a, A->a, is a cyclic dependence, in fact, can be regarded as "detecting the existence of ring in the chain list"
AC Code:
#include <iostream> #include <set> #include <cstring>using namespace Std;int main () {int procs[10000]; int Nproc, NDEP; while (Cin >> Nproc >> ndep) {memset (procs,0,10000); Set<int> IDSet; BOOL hascircle = false; for (; ndep>0;--ndep) {int A, B; Cin >> a >>b; Idset.insert (a); Idset.insert (b); if (a = = b) {hascircle = true; } Procs[a] = b; } if (!hascircle) {for (Set<int>::iterator II = Idset.begin (); II! = Idset.end (); ii++) {bool visit[10000] = {false}; int p = Procs[*ii]; while (P && visit[p] = = False) {Visit[p] = true; p = procs[p]; } if (p) { Hascircle = true; Break }}} if (hascircle) {cout << "NO" << Endl; }else{cout << "YES" << Endl; }} return 0;}
Best Coder round#25 1001 Dependency Detection