当前位置:网站首页>AcWing 1184. 欧拉回路 题解(欧拉回路)
AcWing 1184. 欧拉回路 题解(欧拉回路)
2022-07-21 20:03:00 【QingQingDE23】
AcWing 1184. 欧拉回路
这道题中直接利用入度、出度判断是否为欧拉图的方法要记住,还要记住搜索路径并保存输出的方法
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, M = 4e5 + 10;
int h[N], e[M], ne[M], idx;
int ans[M]; //储存答案路径
int din[N], dout[N]; //储存每个点的入度、出度
int n, m;
int type; //记录图的类型
int used[M];
int cnt; //记录换上有多少边
void add(int a, int b){
e[idx] = b;
ne[idx] = h[a];
h[a] = idx ++ ;
}
void dfs(int u){
//从点开始遍历
for(int &i = h[u]; ~i;){
//i在这里仍然是点的编号,加个&可以避免重复申请空间,节省时间
if(used[i]){
i = ne[i];
continue;
}
//标记这条边用过了
used[i] = true;
if(type == 1) used[i ^ 1] = true;
int t;
//点转化为边
if(type == 1){
//如果是无向图
t = i / 2 + 1;
if(i & 1) t = -t; //如果他是由终点过来的那就是反边
}
else t = i + 1; //如果是有向图,边的编号就是点的编号+1
int j = e[i];
i = ne[i];
dfs(j); // 一直走到底 把终点先压进栈 然后回溯往回走把中间及起点加进栈 最终输出结果 ans[cnt]为起点
ans[ ++ cnt] = t;
}
}
int main()
{
scanf("%d", &type);
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
for(int i = 0; i < m; i ++ ){
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
if(type == 1) add(b, a);
dout[a] ++ , din[b] ++ ;
}
//不能构成欧拉图的情况
if(type == 1){
//如果是wu向图
for(int i = 1; i <= n; i ++ ){
int t = din[i] + dout[i];
if(t & 1){
puts("NO");
return 0;
}
}
}
else{
//如果是you向图
for(int i = 1; i <= n; i ++ ){
if(din[i] != dout[i]){
puts("NO");
return 0;
}
}
}
//能构成欧拉图,深搜遍历输出方案
for(int i = 1; i <= n; i ++ ){
if(h[i] != -1){
dfs(i);
break; //找到一个非孤立点就可以开始遍历
}
}
if(cnt < m){
puts("NO");
return 0;
}
cout<<"YES"<<endl;
for(int i = cnt; i; i -- ){
cout<<ans[i]<<' ';
}
cout<<endl;
return 0;
}
边栏推荐
- MATLAB中split函数使用
- 美参议院初步通过520亿美元「芯片法案」,她竟乘机「投资炒股」!
- 榜样访谈——曾钰倬:从讲座中收获经验
- Is it safe to open an account on tonghuashun online? How to buy national debt reverse repurchase
- File operation - C language
- Unity2d arrow animation
- [ERR] 1273 - Unknown collation: ‘utf8mb4_0900_ai_ci‘
- 【10点公开课】:云视频会议系统私有化实践
- Use of Zen
- Ah, ah, ah? Who is the percentage of margin top relative to
猜你喜欢
基于oracle数据库存储过程的创建及调用
Idea 2022.2 was officially released, and I can't keep up with it!
面试三(多进程调用同一个动态库问题)
How to carry out the anti disclosure work of enterprise source code
视频提取关键帧工具类KeyFramesExtractUtils.py,动态支持三种取帧方式,关键参数可配置,代码经过优化处理,效果和性能更好。
每日一题C语言9
力扣------统计有序矩阵中的负数
文件操作——C语言
GL_TRIANGLE_FAN和GL_TRIANGLE_STRIP
[unity3d] blood bar (HP)
随机推荐
Women's health and health information network dream weaving template (with mobile terminal) [test can be built]
《Service Worker 指南-1》
加载RainCitySpaces的标签信息并进行显示
Intelligent operation and maintenance scenario analysis: how to detect abnormal business system status through exception detection
js 模拟form表单post提交
unordered_map的使用
:empty伪类代替js,实现为空时的提示
禅道的使用
这家初创公司想看看你的丁丁!他们用AI检测性疾病,保证数据匿名且加密,你愿意吗
Weekly recommended short video: what is the difference between IOT development and embedded development?
杭州动环监控系统供应商,动环监控设备
[unity3d] blood bar (HP)
2022 音视频技术风向标
2022 audio and video technology vane
systemd 管理 process-exporter
What is a video content recommendation engine?
【Unity3D】血条(HP)
What is a video content recommendation engine?
RESTful-URL设计规范
: empty pseudo class replaces JS to realize the prompt when it is empty