当前位置:网站首页>欧拉路径与欧拉回路
欧拉路径与欧拉回路
2022-08-01 22:42:00 【秦小咩】
经过所有边一次的路径叫做欧拉路径(一笔画),若路径起点和终点相同,称之为欧拉回路
判定条件
有向图欧拉路径 一个起点(出度-入度=1),一个终点(入度-出度=1),剩余节点(出度=入度)
有向图欧拉回路 所以节点(入度=出度)
无向图欧拉路径 一个起点(度数=奇数),一个终点(度数=奇数),剩余节点(度数为偶数)
无向欧拉回路, 所有节点度数都是偶数
以上四条都建立在将有向边视为无向边后,图是连通图
有向图中的欧拉回路与欧拉路径
# include<iostream>
# include<algorithm>
# include<map>
# include<vector>
# include<stack>
using namespace std;
int nowid[100000+10];
int ru[100000+10],chu[100000+10];
int n,m;
stack<int>st;
vector<int>v[100000+10];
void dfs(int now)
{
for(int i=nowid[now];i<v[now].size();i=nowid[now])
{
nowid[now]=i+1;
dfs(v[now][i]);
}
st.push(now);
}
int main ()
{
cin>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
chu[x]++;
ru[y]++;
v[x].push_back(y);
}
for(int i=1;i<=n;i++)
{
sort(v[i].begin(),v[i].end());
}
int b=1,e,cntb=0,cnte=0;
int cnt=0,flag=0;
for(int i=1;i<=n;i++)
{
if(ru[i]!=chu[i])
{
flag=1;
}
if(chu[i]-ru[i]==1)
{
cntb++;
b=i;
}
else if(ru[i]-chu[i]==1)
{
cnte++;
}
else if(ru[i]==chu[i])
{
cnt++;
}
}
if(!(cnt==n-2&&cntb==1&&cnte==1)&&flag)
{
cout<<"No";
return 0;
}
dfs(b);
while(!st.empty())
{
cout<<st.top()<<" ";
st.pop();
}
return 0;
}
无向图的,没保证连通性,还需要判断连通性,但貌似本题不判断也可以。
另外无序字母对不相同说明标记一次边就够了
# include<iostream>
# include<algorithm>
# include<map>
# include<vector>
# include<stack>
using namespace std;
vector<int>v[1000];
int du[1000];
bool mp[1000][1000];
stack<int>st;
void dfs(int now)
{
for(auto it:v[now])
{
if(!mp[it][now])
{
mp[it][now]=1;
mp[now][it]=1;
dfs(it);
}
}
st.push(now);
}
int main ()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
du[s[0]]++;
du[s[1]]++;
v[s[0]].push_back(s[1]);
v[s[1]].push_back(s[0]);
}
for(int i=1;i<=200;i++)
{
sort(v[i].begin(),v[i].end());
}
int cnt=0;
int b=999;
for(int i=1;i<=200;i++)
{
if(du[i]&1)
{
cnt++;
b=min(b,i);
}
}
if(cnt==0)
{
b=999;
for(int i=1;i<=200;i++)
{
if(du[i])
{
b=i;
break;
}
}
if(b==999)
{
cout<<"No Solution";
}
else
{
dfs(b);
while(!st.empty())
{
cout<<(char)(st.top());
st.pop();
}
}
}
else
{
if(cnt==2)
{
dfs(b);
while(!st.empty())
{
cout<<(char)(st.top());
st.pop();
}
}
else
{
cout<<"No Solution";
return 0;
}
}
return 0;
}[USACO3.3]骑马修栅栏 Riding the Fences - 洛谷
和上一题不同的是,本题无向边多个
#include<iostream>
#include<cstdio>
#include<cmath>
#include<stack>
using namespace std;
int edge[600][600];
int n;
int du[1000];
stack<int>st;
void dfs(int now)
{
for(int i=1;i<=500;i++)
{
if(edge[now][i])
{
edge[now][i]--;
edge[i][now]--;
dfs(i);
}
}
st.push(now);
}
int main()
{
cin>>n;
for(int i=1; i<=n; i++)
{
int x,y;
cin>>x>>y;
edge[x][y]++;
edge[y][x]++;
du[x]++;
du[y]++;
}
for(int i=1;i<=500;i++)
{
if(du[i]&1)
{
dfs(i);
while(!st.empty())
{
cout<<st.top()<<'\n';
st.pop();
}
return 0;
}
}
for(int i=1;i<=500;i++)
{
if(du[i])
{
dfs(i);
while(!st.empty())
{
cout<<st.top()<<'\n';
st.pop();
}
return 0;
}
}
return 0;
}边栏推荐
- npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)
- excel remove all carriage return from a cell
- 数据分析04
- Safe fifth after-school exercise
- seaborn笔记:可视化统计关系(散点图、折线图)
- 2022 edition of MySQL tutorial, top collection good, take your time
- 1. @Component注解的原理剖析
- dvwa 通关记录1 - 暴力破解 Brute Force
- 03. GO language variable definition, function
- From 0 to 1: Design and R&D Notes of Graphic Voting Mini Program
猜你喜欢

JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method

小程序毕设作品之微信体育馆预约小程序毕业设计成品(2)小程序功能

npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)

复现gallerycms字符长度限制短域名绕过

y84.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十五)

(翻译)按钮的对比色引导用户操作的方式

牛客多校4 A.Task Computing 思维

User Experience | How to Measure User Experience?

毫秒级!千万人脸库快速比对,上亿商品图片检索,背后的极速检索用了什么神器?

域名重定向工具 —— SwitchHosts 实用教程
随机推荐
文件查询匹配神器 【glob.js】 实用教程
number of solutions to solve a multivariate multi-degree equation
leetcode 204. Count Primes 计数质数 (Easy)
46.全排列
使用Jenkins做持续集成,这个知识点必须要掌握
When solving yolov5 training: "AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train"
03. GO language variable definition, function
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D 题解
2022年最新河北建筑八大员(机械员)模拟考试题库及答案
数据增强--学习笔记(图像类,cnn)
APP专项测试:流量测试
excel change cell size
excel vertical to horizontal
SOM Network 1: Principles Explained
小程序中的多表联合查询
[机缘参悟-58]:《素书》-5-奉行仁义[遵义章第五]
xctf attack and defense world web master advanced area webshell
npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)
long investment career
如何给 UE4 场景添加游戏角色