You've got a undirected graph G, consisting of n nodes. We will consider the nodes of the graph indexed by integers from 1 to n. We know that each node of graph G is connected by edges with at least k other nodes of this graph. Your task is to find in the given graph a simple cycle of length of at least k + 1.

A simple cycle of length d (d > 1) in graph G is a sequence of distinct graph nodes v1, v2, ..., vd such, that nodes v1 and vd are connected by an edge of the graph, also for any integer i (1 ≤ i < d) nodes vi and vi + 1 are connected by an edge of the graph.

Input

The first line contains three integers nmk (3 ≤ n, m ≤ 105; 2 ≤ k ≤ n - 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. Next m lines contain pairs of integers. The i-th line contains integers aibi (1 ≤ ai, bi ≤ nai ≠ bi) — the indexes of the graph nodes that are connected by the i-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 least k other nodes of the graph.

Output

In the first line print integer r (r ≥ k + 1) — the length of the found cycle. In the next line print r distinct integers v1, v2, ..., vr (1 ≤ vi ≤ n)— 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.

Examples

input

Copy

3 3 2
1 2
2 3
3 1

output

Copy

3
1 2 3

input

Copy

4 6 3
4 3
1 2
1 3
1 4
2 3
2 4

output

Copy

4
3 4 1 2

做这个题,我一开始想的是floyd求最小环,dijkstra求最小环,但是,到后来我发现,数据量太大,而且这个题之说找到大于K的环,便搁置了一下,太难了,后来想到了哈密尔顿,不就一个搜索题,比哈密尔顿路还简单,搜一个回路,并且记录路径,搜到重复点,就是回路,路径点大于K,就是要的答案,直接交,ojbk。

#include<iostream>
#include<queue>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<bitset>
#include<cstdio>
#include<cstring>
//---------------------------------Sexy operation--------------------------// #define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define cins(s) scanf("%s",s)
#define coui(n) printf("%d",n)
#define couc(n) printf("%c",n)
#define coul(n) printf("%lld",n)
#define speed ios_base::sync_with_stdio(0)
#define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
//-------------------------------Actual option------------------------------// #define Swap(a,b) a^=b^=a^=b
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
#define mem(n,x) memset(n,x,sizeof(n))
#define mp(a,b) make_pair(a,b)
//--------------------------------constant----------------------------------// #define INF 0x3f3f3f3f
#define maxn 100005
#define esp 1e-9
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
//------------------------------Dividing Line--------------------------------//
vector<int> ege[maxn],ans;
int vis[maxn];
int n,m,k,cnt;
bool dfs(int u)
{
vis[u]=++cnt;
ans.push_back(u);
for(int i=0;i<ege[u].size();i++)
{
int v=ege[u][i];
if(vis[v])
{
if(cnt-vis[v]<k) continue;
cout<<vis[u]-vis[v]+1<<endl;
for(int i=vis[v];i<=vis[u];i++) cout<<ans[i-1]<<' ';
return puts(""),1;
}
if(dfs(v))return 1;
}
return 0;
}
int main()
{
cin>>n>>m>>k;
for(int i=0;i<m;i++)
{
int x,y;
cini(x),cini(y);
ege[x].push_back(y);
ege[y].push_back(x);
}
dfs(1);
return 0;
}

Codeforce 263D Cycle in Graph 搜索 图论 哈密尔顿环的更多相关文章

  1. CF1221G Graph And Numbers(折半搜索+图论)

    答案=总数-无0-无1-无2+无01+无02+无12-无012 直接详细讲无0和无2 无0为 01和11,无2为01和00,显然二者方案数相同,以下考虑无0 考虑折半搜索,后半段搜索,二进制点权0的位 ...

  2. hihoCoder-1087 Hamiltonian Cycle (记忆化搜索)

    描述 Given a directed graph containing n vertice (numbered from 1 to n) and m edges. Can you tell us h ...

  3. CodeForce 117C Cycle DFS

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

  4. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  5. Graph cuts图论分割

    Graph cuts是一种十分有用和流行的能量优化算法,在计算机视觉领域普遍应用于前背景分割(Image segmentation).立体视觉(stereo vision).抠图(Image matt ...

  6. HDU 4467 Graph(图论+暴力)(2012 Asia Chengdu Regional Contest)

    Description P. T. Tigris is a student currently studying graph theory. One day, when he was studying ...

  7. NOIp2013D2T3 华容道【搜索&图论-最短路】

    题目传送门 暴力搜索 看到这道题的第一反应就是直接上$bfs$啦,也没有想到什么更加优秀的算法. 然后就是$15$分钟打了$70$分,有点震惊,纯暴力诶,这么多白给分嘛,太划算了,这可是$D2T3$诶 ...

  8. Codeforces Round #161 (Div. 2) D. Cycle in Graph(无向图中找指定长度的简单环)

    题目链接:http://codeforces.com/problemset/problem/263/D 思路:一遍dfs即可,dp[u]表示当前遍历到节点u的长度,对于节点u的邻接点v,如果v没有被访 ...

  9. D. Maximum Diameter Graph 贪心+图论+模拟

    题意:给出n个点的度数列 上限(实际点可以小于该度数列)问可以构造简单路最大长度是多少(n个点要连通 不能有平行边.重边) 思路:直接构造一条长链  先把度数为1的点 和度数大于1的点分开  先把度数 ...

随机推荐

  1. web系统安全运营之基础- 基于DFA算法的高性能的敏感词,脏词的检测过滤算法类(c#).

    [概述]做好一个web系统的安全运维,除了常规的防注入,防入侵等,还有一个检测并过滤敏感词,脏词..  这件事做得不好,轻则导致一场投诉或纠纷,重则导致产品被勒令关闭停运. 废话少说,先看下代码,可以 ...

  2. Python Requests-学习笔记(8)-重定向与请求历史

    重定向与请求历史 默认情况下,除了 HEAD, Requests会自动处理所有重定向. 可以使用响应对象的 history 方法来追踪重定向. Response.history 是一个:class:R ...

  3. hadoop(七)集群配置同步(hadoop完全分布式四)|9

    前置配置:rsync远程同步|xsync集群分发(hadoop完全分布式准备三)|9 1. 分布式集群分配原则 部署分配原则 说明Namenode和secondarynamenode占用内存较大,建议 ...

  4. VulnHub靶场学习_HA: InfinityStones

    HA-InfinityStones Vulnhub靶场 下载地址:https://www.vulnhub.com/entry/ha-infinity-stones,366/ 背景: 灭霸认为,如果他杀 ...

  5. Docker+Cmd+Cli+Git之前端工程化纪要(二)自定义类package.json文件管理模块包

    全新升级后的FE工作流为:使用FE命令包进行项目的初始化,其中包括项目初始化.拉取脚手架.私库拉取模块包或后期扩展的CI/CD等与本公司工作流相关的操作. 出现的问题如下: 脚手架工具的包依赖信息存放 ...

  6. 一篇文章快速搞懂Redis的慢查询分析

    什么是慢查询? 慢查询,顾名思义就是比较慢的查询,但是究竟是哪里慢呢?首先,我们了解一下Redis命令执行的整个过程: 发送命令 命令排队 命令执行 返回结果 在慢查询的定义中,统计比较慢的时间段指的 ...

  7. 5. iphone 的:active样式

    如果给按钮定义 :hover 样式,在 iPhone 上按钮点击一次是 hover 态,再点击一次 hover 态才会消失,这不是我们想要的,继而想通过定义 :active 样式来实现按钮按下时的效果 ...

  8. WEBMIN(CVE-2019-15107) 学习

    简单介绍: Webmin是目前功能最强大的基于Web的Unix系统管理工具.管理员通过浏览器访问Webmin的各种管理功能并完成相应的管理动作.目前Webmin支持绝大多数的Unix系统,这些系统除了 ...

  9. [PHP]PHP设计模式:单例模式

    单例模式(职责模式): 简单的说,一个对象(在学习设计模式之前,需要比较了解面向对象思想)只负责一个特定的任务: 单例类: 1.构造函数需要标记为private(访问控制:防止外部代码使用new操作符 ...

  10. redis: 乐观锁(十)

    监视:watch 正常业务(单线程): 127.0.0.1:6379> set money 100 #模拟存款100元 OK 127.0.0.1:6379> set moneyout 0 ...