http://poj.org/problem?id=2230
C++版本一
参考文章:https://blog.csdn.net/weixin_43272781/article/details/88553573
题解:vector 存边+欧拉回路
/*
*@Author: STZG
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int vis[N];
char str;
struct node{
int id,v;
}x;
vector<node> G[N];
void dfs(int u){
for(int i=0,j=G[u].size();i<j;i++){
node tmp=G[u][i];
if(vis[tmp.id])
continue;
vis[tmp.id]=1;
dfs(tmp.v);
}
printf("%d\n",u);
}
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
//scanf("%d",&t);
//while(t--){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d",&u,&v);
x.v=v;
x.id=2*i-1;
G[u].push_back(x);
x.v=u;
x.id=2*i;
G[v].push_back(x);
}
dfs(1);
//}
#ifdef DEBUG
printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
//cout << "Hello world!" << endl;
return 0;
}
C++版本二
题解:链式前向星存边+欧拉回路
#include<iostream>
#include<cstdio>
#define M 50010
#define N 10010
using namespace std;
struct edge_node
{
int to,next;
bool vis;
}e[M*2];
int head[N];
int cnt;
void ins(int u,int v)
{
e[++cnt].to = v; e[cnt].next = head[u]; head[u] = cnt;
e[cnt].vis = false;
}
void find(int u)
{
for (int i=head[u];i;i=e[i].next)
{
if (!e[i].vis)
{
e[i].vis = true;
find(e[i].to);
}
}
printf("%d\n",u);
}
int main()
{
int n,m,u,v;
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
ins(u,v);
ins(v,u);
}
find(1);
}