Codeforce 263D Cycle in Graph 搜索 图论 哈密尔顿环
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 n, m, k (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 ai, bi (1 ≤ ai, bi ≤ n; ai ≠ 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;
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 详解Kubernetes存储体系
- Python3 正则表达式特殊符号及用法.md
- Web网页响应式布局.md
- BootStrap应用开发学习入门1
- Python实现单链表和字典
- 关于如何做好运维管理工作的一点思考
- 2.6-Air302(NB-IOT)-基础外设-AD采集,NTC温度传感器B3950
- 2.5-Air302(NB-IOT)-基础外设-串口(485,422),CRC校验
- 2.3-Air302(NB-IOT)-基础外设-输入检测
- 2.2-Air302(NB-IOT)-基础外设-PWM输出
- API可视化管理平台YApi
- BootStrap应用开发学习入门
- 为什么要学习现代控制理论(机器人方向)?
- 2.XML之编程解析示例笔记
- XML快速入门学习笔记