当前位置:网站首页>欧拉路径与欧拉回路
欧拉路径与欧拉回路
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;
}
边栏推荐
- excel vertical to horizontal
- From 0 to 1: Design and R&D Notes of Graphic Voting Mini Program
- Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D Solution
- User Experience | How to Measure User Experience?
- Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (4) Opening Report
- 一种灵活的智能合约协作方式
- 如何使用pywinauto和pyautogui将动漫小姐姐链接请回家
- 线上故障排查方案
- leetcode 204. Count Primes 计数质数 (Easy)
- 力扣第 304 场周赛复盘
猜你喜欢
关于ETL的两种架构(ETL架构和ELT架构)
小程序毕设作品之微信体育馆预约小程序毕业设计成品(3)后台功能
10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
Still struggling with reporting tool selection?To take a look at this
Flutter基础学习(一)Dart语言入门
Deep Learning Course2 Week 2 Optimization Algorithms Exercises
Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (4) Opening Report
Go 微服务开发框架DMicro的设计思路
Quarantine and downgrade
13、学习MySQL 分组
随机推荐
杭电多校3 1012. Two Permutations dp*
xctf攻防世界 Web高手进阶区 webshell
When solving yolov5 training: "AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train"
华为无线设备配置双链路冷备份(AP指定配置方式)
从0到100:招生报名小程序开发笔记
[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)
PAM Palindromic Automata
excel vertical to horizontal
APP special test: traffic test
13、学习MySQL 分组
excel split text into different rows
String - Trie
long investment career
[Recommended books] The first self-driving technology book
JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method
RxJs SwitchMapTo 操作符之移花接木
基于 OData 模型和 JSON 模型的 SAP UI5 表格控件行项目的添加和删除实现
Still struggling with reporting tool selection?To take a look at this
2022-08-01 第八组 曹雨 泛型 枚举
Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you