Test instructions: Give a direction graph, ask is not cactus diagram. Cactus diagram: A strongly connected graph of each side only within a normal ring.
Solution: Tarjan Determine whether the strong connected component is 1, record the path of the loop, the number of times to traverse the path record point when each ring is found, if a bit is recorded two times, it means that there is not only one ring in the edge.
Code:
#include <stdio.h>#include<iostream>#include<algorithm>#include<string>#include<string.h>#include<math.h>#include<limits.h>#include<time.h>#include<stdlib.h>#include<map>#include<queue>#include<Set>#include<stack>#include<vector>#defineLL Long Longusing namespacestd;intN, M;vector<int> edge[20005];intdfn[20005], low[20005];intCNT =1;intAns =0;intfa[20005], out[20005];intFlag =1;voidSignintVintu) { while(Fa[u]! =v) { out[u]++; if( out[U] >1) {flag=0; return ; } u=Fa[u]; }}voidTarjan (intv) {Dfn[v]= Low[v] = cnt++; intLen =edge[v].size (); for(inti =0; i < Len; i++) { intU =Edge[v][i]; if(!Dfn[u]) {Fa[u]=v; Tarjan (U); LOW[V]=min (Low[v], low[u]); } Else{Low[v]=min (Low[v], dfn[u]); sign (U, v); if(!flag)return ; } } if(Dfn[v] = =Low[v]) { inttmp; Ans++; if(Ans >1) {flag=0; return ; } }}intMain () {intT; scanf ("%d", &T); while(t--) {CNT=1; Ans=0; Flag=1; scanf ("%d%d", &n, &m); for(inti =0; I < n; i++) edge[i].clear (); for(inti =0; I < m; i++) { intV, u; scanf ("%d%d", &v, &T); Edge[v].push_back (U); } memset (DFN,0,sizeofDFN); memset (FA,0,sizeofFA); memset ( out,0,sizeof out); for(inti =0; I < n; i++) if(!Dfn[i]) Tarjan (i); if(flag) printf ("yes\n"); Elseprintf ("no\n"); } return 0;}
UVA 10510 Cactus