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

2
6 8 4
0 1
1 2
2 0
3 4
4 5
5 3
1 3
2 4
4 4 3
0 1
1 2
2 3
3 0

Sample Output

YES
NO

HINT

Source


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

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

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <list>
#include <algorithm>
#include <climits>
using namespace std; #define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 200005
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)
const int mod = 1e9+7;
vector<int> a[550];
int vis[550],flag;
int n,m,k; void dfs(int now,int pos,int pre)
{ if(vis[now])
{
if(pos-vis[now]==k)
flag = 1;
return;
}
if(flag)
return;
vis[now]=pos;
int i,len = a[now].size();
for(i = 0; i<len; i++)
{
if(a[now][i]!=pre)
dfs(a[now][i],pos+1,now); }
} int main()
{
int i,j,x,y,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(i = 0; i<=n; i++)
a[i].clear();
flag = 0;
while(m--)
{
scanf("%d%d",&x,&y);
a[x].push_back(y);
a[y].push_back(x);
}
for(i=0; i<n; i++)
{
MEM(vis,0);
dfs(i,1,-1);
}
printf("%s\n",flag?"YES":"NO");
} return 0;
}

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. Linux150个常用指令

    线上查询及帮助命令(2个) 文件和目录操作命令(18个) 查看文件及内容处理命令(21个) 文件压缩及解压缩命令(4个) 信息显示命令(11个) 搜索文件命令(4个) 用户管理命令(10个) 基础网络 ...

  2. SQL中NUMERIC和DECIMAL的区别

    numeric 和 decimal 数据类型的默认最大精度值是 38.在 Transact-SQL 中,numeric 与 decimal 数据类型在功能上等效. decimal(numeric ) ...

  3. 在performancepoint里面建立数据源的时候,总是发生以下的报警

    就是我在performancepoint里面建立数据源的时候,总是发生以下的报警.   在管理中心主页的“应用程序管理”部分,单击“管理服务应用程序”,然后单击“PerformancePoint Se ...

  4. COWRUN

    USACO COWRUN 随机化搜索+双重递归调用 题面描述:给出8*N(<=14)组牌,每次按顺序选择8张,FJ可以选择前4张或者后4张,COW从FJ选出的牌中选择前两张或者后两张,然后COW ...

  5. JAVA 获取访问者IP

    * 获取访问者IP * * 在一般情况下使用Request.getRemoteAddr()即可,但是经过nginx等反向代理软件后,这个方法会失效. * * 本方法先从Header中获取X-Real- ...

  6. 【Henu ACM Round#16 B】 Bear and Colors

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] O(n^2)枚举每一个区间. 然后维护这个区间里面的"统治数字"是什么. 对于每个区间cnt[统治数字]++; ...

  7. Linux学习总结(7)——阿里云centeros服务器上安装 jdk,tomcat,mysql

    查看服务器的系统版本 # cat /etc/issue 查看服务器是64位还是32位 #uname -a      或者用:#getconf LONG_BIT 查看当前有没有安装jdk #rpm -q ...

  8. Add Webhooks to Your API the Right Way

    Add Webhooks to Your API the Right Way Adam DuVander / December 15, 2016 In the last 10 years, APIs ...

  9. CentOS6.4安装Docker

    首先,须要安装EPEL仓库 # wget http://mirrors.yun-idc.com/epel/6/i386/epel-release-6-8.noarch.rpm # rpm -ivh e ...

  10. spring 使用c3po连接池

    1 数据源:能够简单理解为数据的来源. 2 连接池:是缓存一定数量的数据库连接,当程序须要数据库连接的时候,直接在连接池中获取空暇的连接,使用完再放回连接池中,此连接又变成空暇状态,等待下一次连接. ...