Codeforces 263 D. Cycle in Graph, codeforcescycle

Source: Internet
Author: User

Codeforces 263 D. Cycle in Graph, codeforcescycle


DFS again ......

D. Cycle in Graphtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

You 've got a undirected graphG, ConsistingNNodes. We will consider the nodes of the graph indexed by integers from 1N. We know that each node of graphGIs connected by edges with at leastKOther nodes of this graph. Your task is to find in the given graph a simple cycle of length of at leastKUpload + upload 1.

A simple cycle of lengthD(DRows> rows 1) in graphGIs a sequence of distinct graph nodesV1, bytes,V2, middle..., middle ,...,VDSuch, that nodesV1 andVDAre connected by an edge of the graph, also for any integerI(1 digit ≤ DigitILatency <latencyD) NodesVIAndVIKeys + keys 1 are connected by an edge of the graph.

Input

The first line contains three integersN,M,K(3 cores ≤ CoresN, Bytes,MLimit ≤ limit 105; 2 limit ≤ limitKLimit ≤ limitNVertex-vertex 1)-the number of the nodes of the graph, the number of the graph's edges and the lower limit on the degree of the graph node. NextMLines contain pairs of integers.I-Th line contains integersAI,BI(1 digit ≤ DigitAI, Bytes,BILimit ≤ limitN;AI  =BI)-The indexes of the graph nodes that are connected byI-Th edge.

It is guaranteed that the given graph doesn't contain any multiple edges or self-loops. It is guaranteed that each node of the graph is connected by the edges with at leastKOther nodes of the graph.

Output

In the first line print integerR(RLimit ≥ limitKLimit + limit 1)-the length of the found cycle. In the next line printRDistinct integersV1, bytes,V2, middle..., middle ,...,VR(1 digit ≤ DigitVILimit ≤ limitN)-The found simple cycle.

It is guaranteed that the answer exists. If there are multiple correct answers, you are allowed to print any of them.

Sample test (s) input
3 3 21 22 33 1
Output
31 2 3 
Input
4 6 34 31 21 31 42 32 4
Output
43 4 1 2 


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int maxn=100100;bool vis[maxn];int deep[maxn];int n,m,k,cnt;vector<int> edge[maxn];vector<int> road;bool dfs(int u,int dep){    vis[u]=true;    deep[u]=dep;    road.push_back(u);    for(int i=0,sz=edge[u].size();i<sz;i++)    {        int v=edge[u][i];        if(vis[v]==true&&deep[u]-deep[v]>=k)        {            int id=0;            for(int i=0;i<road.size();i++)            {                if(road[i]==v)                {                    id=i; break;                }            }            printf("%d\n",road.size()-id);            for(int i=id,sz=road.size();i<sz;i++)            {                printf("%d%c",road[i],(i+1==sz)?'\n':' ');            }            return true;        }        else if(vis[v]==false)        {            if(dfs(v,dep+1)) return true;        }    }    road.pop_back();    return false;}int main(){    scanf("%d%d%d",&n,&m,&k);    while(m--)    {        int a,b;        scanf("%d%d",&a,&b);        edge[a].push_back(b);        edge[b].push_back(a);    }    for(int i=1;i<=n;i++)    {        cnt=0;        if(vis[i]==false)        {            if(dfs(i,cnt)) break;        }    }    return 0;}







Contact Us

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.