跳到正文

个人记录

欧拉路径1

Hhy140516约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年7月19日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 6 分钟

版本

  1. v1当前

基础

有向图

对于欧拉通路,除去起点和终点之外,其他所有点,入度=出度。

对于欧拉回路,所有的点,入度=出度。

无向图

对于欧拉通路,除去起点和终点之外,其他所有点,度为偶数(一对边)。

对于欧拉回路,所有的点,度为偶数(一对边)。

欧拉图

带欧拉回路的图。

欧拉半图

带欧拉通路的图,不带回路。

希尔霍尔策(hierholzer)

快速去接具体的欧拉路径问题的算法。

步骤

  1. 找到起点,出发的简单环的答案。
  2. 将中间得到的所有简单环,插入路径之中。

算法步骤

  1. 从起点出发,遍历所有边,并且标记。
  2. 遍历所有边结束后,起点才入栈。

简单图

每个点只经过一次的回路。

链式前向星

如果两条边属于同一条无向边的边的编号分别为 ii 和 i∧1i \land 1。

因为重边的缘故,处理无向图的数据结构一般性都需要使用链式前向星。

例题

P7771 【模板】欧拉路径

用 headhead 数组存储走没走过这条边,做 dfs。

C++
//Code by hhy
#include<bits/stdc++.h>
#define F(i , a , b , c) for( int i = (a) ; ((c > 0) ? i <= (b) : i >= (b)) ; i += c )
#define T(i , root , b , c) for( int i = root ; b ; c )
#define int long long
#define W(f) while(f)
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ll long long
#define debug(...){\
	cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
	string s=#__VA_ARGS__,s2="";\
	vector<string>v;\
	for(auto i:s){\
		if(i==','){\
			v.push_back(s2);\
			s2="";\
		}else{\
			s2+=i;\
		}\
	}\
	v.push_back(s2);\
	vector<int>v2={__VA_ARGS__};\
	for(int i=0;i<v.size()-1;i++){\
		cout<<v[i]<<"="<<v2[i]<<"\n";\
	}\
	cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
using namespace std ;
const int kMaxN = 2e5 + 5 , MOD = 998244353 , INF = 1e18 ;
struct Edgepr
{
	int u , w ;
};
struct Edgeve
{
	int v , w ;
};
struct node
{
  int x , y , id ;
}Node[kMaxN] ;
int n , m ;
vector<int> G[200005] , ans , bis ;
int dep[200005][2] , head[200005] ;
inline ll ksm(ll a , ll b)
{
	ll mul = 1 ;
	while(b)
	{
		if(b & 1) mul *= a , mul %= MOD ;
		a *= a ;
		a %= MOD ;
		b >>= 1 ;
	}
	return mul ;
}
inline int read()
{
    int x = 0 , f = 1 ;
    char ch = getchar() ;
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') f = -1 ;
        ch = getchar() ;
    }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0' , ch = getchar() ;
    return x * f ;
}
void write(int x)
{
    if(x < 0) putchar('-') , x = -x ;
    if(x > 9) write(x / 10) ;
    putchar(x % 10 + '0') ;
}
void dfs(int u)
{
	
	for( int i = head[u] ; i < G[u].size() ; i = head[u] )
	{
		head[u] = i + 1 ;
		dfs(G[u][i]) ;
	}
	ans.push_back(u) ;
}
void Read()
{
	
}
void work()
{
  Read() ;
	cin >> n >> m ;
	for( int i = 1 ; i <= m ; i++ )
	{
		int u , v ;
		cin >> u >> v ;
		dep[u][0]++ ;
		dep[v][1]++ ;
		G[u].push_back(v) ;
	}
	int st = 1 , c0 = 0 , c1 = 0 ;
	for( int i = 1 ; i <= n ; i++ )
	{
		if(dep[i][0] != dep[i][1])
		{
			if(dep[i][0] - dep[i][1] == 1)
			{
				c0++ ;
				st = i ;
			}
			if(dep[i][1] - dep[i][0] == 1)
			{
				c1++ ;
			}
		}
	} 
	if((c0 == 0 && c1 == 0) == 0 && (c0 == 1 && c1 == 1) == 0)
	{
		cout << "No" ;
		return ;
	}
	for( int i = 1 ; i <= n ; i++ ) sort(G[i].begin() , G[i].end()) ;
	dfs(st) ;
	for( int i = ans.size() - 1 ; i >= 0 ; i-- )
	{
		cout << ans[i] << " " ;
	}
}
signed main()
{
//	freopen(".in" , "r" , stdin) ;
//	freopen(".out" , "w" , stdout) ;
	ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
  int t = 1 ;
  // cin >> t ;
  while(t--) work() ;
  return 0 ;
}
原文