# CSU 1660 K-cycle (Dfs determines if there is a ring of length K in the non-graph)

Source: Internet
Author: User

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)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.