【图论】欧拉回路

本文探讨了图论中的欧拉回路概念,重点关注无向图的情况。通过实例分析,阐述如何寻找和理解欧拉回路,并提供了一个在线平台hihocoder-1181的相关练习。

先上一个板子吧,无向的
hihocoder-1181

#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const int maxn=1e4+5;
struct E{
	int to,next;
}edge[maxn<<1];
int head[maxn],tot,vis[maxn<<1],path[maxn],top;
int n,m;
void add(int u,int v){
	edge[tot].to=v;
	edge[tot].next=head[u];
	head[u]=tot++;
}
void dfs(int u){
	for(int i=head[u];i!=-1;i=edge[i].next){
		if(!vis[i]){
			vis[i]=1;
			vis[i^1]=1;
			dfs(edge[i].to);
		}
	}
	path[top++]=u;
}
int main(){
	memset(head,-1,sizeof(head));
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int u,v;cin>>u>>v;
		add(u,v);add(v,u);
	}
	dfs(1);
	for(int i=0;i<top-1;i++){
		cout<<path[i]<<" ";
	}
	cout<<path[top-1]<<endl; 
}
/*
6 7
1 2
1 4
2 3
2 6
2 5
3 4
5 6
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值