Test instructions: Determines whether all points in a given direction graph can reach each other.
is to ask if there is only one strong connected component.
1#include <stdio.h>2#include <string.h>3#include <stack>4#include <queue>5 using namespacestd;6 7 Const intmaxn=1e4+5;8 Const intmaxm=1e5+5;9 Ten inthead[maxn],point[maxm],nxt[maxm],size; One intn,t,scccnt; A intSTX[MAXN],LOW[MAXN],SCC[MAXN]; -stack<int>S; - the voidinit () { -memset (head,-1,sizeof(head)); -Size=0; - } + - voidAddintAintb) { +point[size]=b; Anxt[size]=Head[a]; athead[a]=size++; - } - - voidDfsints) { -stx[s]=low[s]=++T; - S.push (S); in for(intI=head[s];~i;i=Nxt[i]) { - intj=Point[i]; to if(!Stx[j]) { + Dfs (j); -low[s]=min (low[s],low[j]); the } * Else if(!Scc[j]) { $low[s]=min (low[s],stx[j]);Panax Notoginseng } - } the if(low[s]==Stx[s]) { +scccnt++; A while(1){ the intu=S.top (); S.pop (); +scc[u]=scccnt; - if(S==u) Break; $ } $ } - } - the voidSETSCC () { -memset (STX,0,sizeof(STX));Wuyimemset (SCC,0,sizeof(SCC)); theT=scccnt=0; - for(intI=1; i<=n;++i)if(!Stx[i]) DFS (i); Wu } - About intMain () { $ intm; - while(SCANF ("%d%d", &n,&m)!=eof&&n+m) { - init (); - while(m--){ A intb; +scanf"%d%d",&a,&b); the Add (A, b); - } $ SETSCC (); the if(scccnt==1) printf ("yes\n"); the Elseprintf"no\n"); the } the return 0; -}
View Code
hdu1269 Strong Connectivity