#include <iostream>#include<stdio.h>#include<algorithm>#include<vector>#include<string>#include<string.h>using namespacestd;Const intMaxx =100010;intN,m;vector<int>G[maxx];vector<int>Rg[maxx];vector<int>vs;BOOLVis[maxx];intCmp[maxx];voidDfsintv) {Vis[v]=true; for(intI=0; I<g[v].size (); i++) { if(!Vis[g[v][i]]) DFS (g[v][i]); } vs.push_back (v);}voidRdfsintv) {Vis[v]=true; for(intI=0; I<rg[v].size (); i++) { if(!Vis[rg[v][i]] Rdfs (rg[v][i]); }}intMain () { while(SCANF ("%d%d", &n,&m) && (n+m)! =0) { for(intI=0; i<=n;i++) {g[i].clear (); Rg[i].clear (); } vs.clear (); for(intI=0; i<m;i++) { intTMP1,TMP2; scanf ("%d%d",&tmp1,&TMP2); G[tmp1].push_back (TMP2); Rg[tmp2].push_back (TMP1); } memset (Vis,0,sizeof(VIS)); for(intI=1; i<=n;i++) { if(!Vis[i]) {DFS (i); }} memset (Vis,0,sizeof(VIS)); intk=0; for(intI=vs.size ()-1; i>-1; i--) { if(!Vis[vs[i]]) {Rdfs (vs[i]); K++; } } if(k==1) printf ("yes\n"); Elseprintf"no\n"); } return 0;}
First put the code here, and later have time to fill the learning process.
Finding Strong connected components