Description

A simple cycle is a closed simple path, with no other repeated vertices or edges other than the starting and ending vertices. The length of a cycle is the number of vertices on it. Given an undirected graph G(V, E), you are to detect whether it contains a simple
cycle of length K. To make the problem easier, we only consider cases with small K here.

Input

There are multiple test cases.

The first line will contain a positive integer T (T ≤ 10) meaning the number of test cases.

For each test case, the first line contains three positive integers N, M and K ( N ≤ 50, M ≤ 500, 3 ≤ K ≤ 7). N is the number of vertices of the graph, M is the number of edges and K is the length of the cycle desired. Next follow M lines, each line contains
two integers A and B, describing an undirected edge AB of the graph. Vertices are numbered from 0 to N-1.

Output

For each test case, you should output “YES” in one line if there is a cycle of length K in the given graph, otherwise output “NO”.

Sample Input

  1. 2
  2. 6 8 4
  3. 0 1
  4. 1 2
  5. 2 0
  6. 3 4
  7. 4 5
  8. 5 3
  9. 1 3
  10. 2 4
  11. 4 4 3
  12. 0 1
  13. 1 2
  14. 2 3
  15. 3 0

Sample Output

  1. YES
  2. NO

HINT

Source


题意:
问在一个图里面是否能找到一个长度为k的环

思路:
直接搜索看点是否反复訪问

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <string>
  5. #include <stack>
  6. #include <queue>
  7. #include <map>
  8. #include <set>
  9. #include <vector>
  10. #include <math.h>
  11. #include <bitset>
  12. #include <list>
  13. #include <algorithm>
  14. #include <climits>
  15. using namespace std;
  16.  
  17. #define lson 2*i
  18. #define rson 2*i+1
  19. #define LS l,mid,lson
  20. #define RS mid+1,r,rson
  21. #define UP(i,x,y) for(i=x;i<=y;i++)
  22. #define DOWN(i,x,y) for(i=x;i>=y;i--)
  23. #define MEM(a,x) memset(a,x,sizeof(a))
  24. #define W(a) while(a)
  25. #define gcd(a,b) __gcd(a,b)
  26. #define LL long long
  27. #define N 200005
  28. #define INF 0x3f3f3f3f
  29. #define EXP 1e-8
  30. #define lowbit(x) (x&-x)
  31. const int mod = 1e9+7;
  32. vector<int> a[550];
  33. int vis[550],flag;
  34. int n,m,k;
  35.  
  36. void dfs(int now,int pos,int pre)
  37. {
  38.  
  39. if(vis[now])
  40. {
  41. if(pos-vis[now]==k)
  42. flag = 1;
  43. return;
  44. }
  45. if(flag)
  46. return;
  47. vis[now]=pos;
  48. int i,len = a[now].size();
  49. for(i = 0; i<len; i++)
  50. {
  51. if(a[now][i]!=pre)
  52. dfs(a[now][i],pos+1,now);
  53.  
  54. }
  55. }
  56.  
  57. int main()
  58. {
  59. int i,j,x,y,t;
  60. scanf("%d",&t);
  61. while(t--)
  62. {
  63. scanf("%d%d%d",&n,&m,&k);
  64. for(i = 0; i<=n; i++)
  65. a[i].clear();
  66. flag = 0;
  67. while(m--)
  68. {
  69. scanf("%d%d",&x,&y);
  70. a[x].push_back(y);
  71. a[y].push_back(x);
  72. }
  73. for(i=0; i<n; i++)
  74. {
  75. MEM(vis,0);
  76. dfs(i,1,-1);
  77. }
  78. printf("%s\n",flag?"YES":"NO");
  79. }
  80.  
  81. return 0;
  82. }

CSU1660: K-Cycle的更多相关文章

  1. 寒假训练——搜索 K - Cycle

    A tournament is a directed graph without self-loops in which every pair of vertexes is connected by ...

  2. 统计学习方法 | 第3章 k邻近法

    第3章 k近邻法   1.近邻法是基本且简单的分类与回归方法.近邻法的基本做法是:对给定的训练实例点和输入实例点,首先确定输入实例点的个最近邻训练实例点,然后利用这个训练实例点的类的多数来预测输入实例 ...

  3. KNN算法与Kd树

    最近邻法和k-近邻法 下面图片中只有三种豆,有三个豆是未知的种类,如何判定他们的种类? 提供一种思路,即:未知的豆离哪种豆最近就认为未知豆和该豆是同一种类.由此,我们引出最近邻算法的定义:为了判定未知 ...

  4. 【AStar】初赛第一场

    1. All X1.1 基本思路k和c的范围都不大,因此可以考虑迭代找循环节,然后求余数,判定是否相等.这题还是挺简单的.1.2 代码 /* 5690 */ #include <iostream ...

  5. leetcode — permutation-sequence

    import java.util.ArrayList; import java.util.List; /** * Source : https://oj.leetcode.com/problems/p ...

  6. 【codevs4919】线段树练习4

    题目大意:维护一个长度为 N 的序列,支持两种操作:区间加,区间查询有多少数是 7 的倍数. 题解:在每个线段树中维护一个权值数组 [0,6],由于个数显然支持区间可加性,因此可用线段树来维护. 代码 ...

  7. LA 7056 Colorful Toy Polya定理

    题意: 平面上给出一个\(N\)个点\(M\)条边的无向图,要用\(C\)种颜色去给每个顶点染色. 如果一种染色方案可以旋转得到另一种染色方案,那么说明这两种染色方案是等价的. 求所有染色方案数 \( ...

  8. django模型操作

    Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表        

  9. LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  10. 【leetcode】Linked List Cycle II (middle)

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

随机推荐

  1. 3.AngularJS-过滤器

    转自:https://www.cnblogs.com/best/p/6225621.html 二.过滤器 使用过滤器格式化数据,变换数据格式,在模板中使用一个插值变量.语法格式如下: {{ expre ...

  2. GOLANG 加密,解密,GUID 小方法

    golang的 MD5加密.BASE64解密  guid 的代码: /** * 用于加密,解密,(包含MD5加密和base64加密/解密)以及GUID的生成 * 时间: * zhifieya */ p ...

  3. HDU 5353 Average 贪心

    就是贪心啊,不知道为啥总是不过,总是WA 方法不对吗? 将数组扩展一倍,从左到右扫描,大于平均数就给右边的,小于就从右边拿,等于就不变,记录下操作类型. 大于2直接NO,不知道哪错了,自己出了一些数据 ...

  4. Docker -- 系统整洁之道 -- 1

    在上文Docker – 系统整洁之道 – 0中已经对Docker是什么,安装Docker以及怎么运行一个简单的容器有了初步了解,这篇文章介绍Docker的一些命令和Docker镜像的使用及操作. 一些 ...

  5. org.mybatis.spring.mapper.MapperScannerConfigurer$Scanner$1

    不能加载或找不到 org.mybatis.spring.mapper.MapperScannerConfigurer$Scanner$1 经查证,是mybatis-spring-xxx.jar 这个版 ...

  6. OPENCV(6) —— 角点检测

    图像特征的类型通常指边界.角点(兴趣点).斑点(兴趣区域).角点就是图像的一个局部特征,应用广泛.harris角点检测是一种直接基于灰度图像的角点提取算法,稳定性高,尤其对L型角点检测精度高,但由于采 ...

  7. js中迭代的常用几种方法

    var arr = [1,3,2,5,3]; //forEach 两个参数,第一个为数组内容,第二个为数组下标arr.forEach(function(item,index) { console.lo ...

  8. Linux下QQ的使用并手动设置QQ文件保存路径

    一.背景&&目标 马化腾迟迟不肯做linux版本的QQ和微信,实在抠脚. 没有办法,要在linux上使用QQ,目前我找到最好的办法就是使用wine,然而wine这个杀千刀的又是个坑货, ...

  9. 转:mac环境下使用svn

    在Windows环境中,我们一般使用TortoiseSVN来搭建svn环境.在Mac环境下,由于Mac自带了svn的服务器端和客户端功能,所以我们可以在不装任何第三方软件的前提下使用svn功能,不过还 ...

  10. 基于SVM的数据分类预測——意大利葡萄酒种类识别

    update:把程序源代码和数据集也附上http://download.csdn.net/detail/zjccoder/8832699 2015.6.24 --------------------- ...