Test instructions: Give you an no-map to determine if there is a ring of length k.
Idea: DFS traversal has a ring with a length of k at each point as the starting point. Now in DFS (NOW,LAST,STEP) represents the current point, last represents the previous point of access, step is a record path length counter, and S "I" records the path length from the starting point to the I point. If a point is accessed a second time, then the ring appears, judging the current path length and whether it is the first time the length difference is equal to K.
#include <cstdio>#include<cstring>using namespacestd;Const intmaxn=Wuyi;BOOLE[maxn][maxn],flag;intS[MAXN];intn,m,k,a,b,t;BOOLDfsintNowintLastintStep) { if(S[now])returnstep-s[now]==K; S[now]=step; for(intI=0; i<n;i++) { if(i!=last&&E[now][i]&&dfs (i,now,step+1))
return true; } return false;}intMain () {scanf ("%d",&t); while(t--) { for(intI=0; i<maxn;i++) { for(intj=0; j<maxn;j++) {E[i][j]=false; }} scanf ("%d%d%d",&n,&m,&k); while(m--) {scanf ("%d%d",&a,&b); E[A][B]=e[b][a]=true; } Flag=false; for(intI=0; i<n;i++) {memset (s),0,sizeof(s)); if(Dfs (I,0,1)) Flag=true; } if(flag) printf ("yes\n"); Elseprintf"no\n"); } return 0;}
CSU 1660 K-cycle (Dfs determines if there is a ring of length K in the non-graph)