当前位置:网站首页>欧拉路径与欧拉回路
欧拉路径与欧拉回路
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;
}
边栏推荐
- 03、GO语言变量定义、函数
- AQS
- Analysis of the development trend of game metaverse
- 工程建筑行业数据中台指标分析
- (翻译)按钮的对比色引导用户操作的方式
- visual studio code multiple editing
- Deep Learning Course2 Week 2 Optimization Algorithms Exercises
- vscode hide menu bar
- Graph Theory - Strongly Connected Component Condensation + Topological Sort
- Postman batch test interface detailed tutorial
猜你喜欢
How to add a game character to a UE4 scene
力扣第 304 场周赛复盘
小程序毕设作品之微信美食菜谱小程序毕业设计成品(6)开题答辩PPT
小程序毕设作品之微信体育馆预约小程序毕业设计成品(4)开题报告
使用分类权重解决数据不平衡的问题
联邦学习入门
Deep Learning Course2 Week 2 Optimization Algorithms Exercises
JS prototype hasOwnProperty in 加方法 原型终点 继承 重写父类方法
威纶通触摸屏如何打开并升级EB8000旧版本项目并更换触摸屏型号?
2022 edition of MySQL tutorial, top collection good, take your time
随机推荐
字符串——Trie
Prufer序列
[深入研究4G/5G/6G专题-48]: 5G Link Adaption链路自适应-4-下行链路自适应DLLA-PDCCH信道
Postman 批量测试接口详细教程
使用分类权重解决数据不平衡的问题
Still struggling with reporting tool selection?To take a look at this
如何理解 new (...args: any[]) => any
[Recommended books] The first self-driving technology book
excel split text into different rows
SQL Server (design database--stored procedure--trigger)
复现gallerycms字符长度限制短域名绕过
PHP算法之最接近的三数之和
解决yolov5训练时出现:“AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train ”
文件查询匹配神器 【glob.js】 实用教程
Create virtual environments with virtualenv and Virtualenvwrapper virtual environment management tools
leetcode 204. Count Primes 计数质数 (Easy)
萍不回答
杭电多校3 1012. Two Permutations dp*
威纶通触摸屏如何打开并升级EB8000旧版本项目并更换触摸屏型号?
10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享