CSU1660: K-Cycle

來源:互聯網
上載者:User

標籤:csu

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
26 8 40 11 22 03 44 55 31 32 44 4 30 11 22 33 0
Sample Output
YESNO
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;        memset(vis,0,sizeof(vis));        while(m--)        {            scanf("%d%d",&x,&y);            a[x].push_back(y);            a[y].push_back(x);        }        for(i=0; i<n; i++)        {            if(!vis[i])                dfs(i,1,-1);        }        printf("%s\n",flag?"YES":"NO");    }     return 0;}


CSU1660: K-Cycle

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.