Strong Connectivity Graph concept: can reach any point from any point * * *
#include <cstdio>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<cstring>#include<vector>using namespacestd;#defineN 10005intN, M;intDfn[n], low[n];intStack[n], instack[n];intTime , top, CNT, Bloks;vector<vector<int> >G;voidInit () {g.clear (); G.resize (n+5); memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(low)); memset (Stack,0,sizeof(Stack)); memset (Instack,0,sizeof(Instack)); time=top=cnt=bloks=0;}voidTarjan (intu) {Dfn[u]=low[u]=++Time ; Stack[top++]=T; Instack[u]=1; intlen=g[u].size (), V; for(intI=0; i<len; i++) {v=G[u][i]; if(!Dfn[v]) {Tarjan (v); Low[u]=min (Low[u], low[v]); } Else if(Instack[v]) {Low[u]=min (Low[u], dfn[v]); } } if(dfn[u]==Low[u]) { Do{cnt++; V=stack[--top]; INSTACK[V]=0; } while(u!=v); Bloks++; }}intMain () { while(SCANF ("%d%d", &n, &m), n+m) {Init (); intA, B; while(m--) {scanf ("%d%d", &a, &b); G[a].push_back (b); } Tarjan (1); if(cnt==n&&bloks==1) printf ("yes\n"); Elseprintf ("no\n"); } return 0;}
Strong connectivity Figure Hdu 1269