题意:给出一个无向图,要求求出一个路径使得走过每条路正反两遍
思路:求有向图的欧拉路径
代码如下:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=100005;
struct Edge
{
int u,v,next;
}edge[maxn];
int tot;
int first[maxn];
bool vis[maxn];
void add(int uu,int vv)
{
edge[tot].u=uu;
edge[tot].v=vv;
edge[tot].next=first[uu];
first[uu]=tot++;
}
int ans[maxn];
int rec;
void dfs(int now)
{
for(int k=first[now];k!=-1;k=edge[k].next)
{
if(!vis[k])
{
vis[k]=true;
dfs(edge[k].v);
ans[rec++]=edge[k].v;
}
}
}
int main()
{
//freopen("data.txt","r",stdin);
memset(vis,0,sizeof(vis));
memset(first,-1,sizeof(first));
int m,n;
tot=0;
rec=0;
scanf("%d%d",&n,&m);
for(int i=0;i<m;++i)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
//ans[rec++]=1;
dfs(1);
ans[rec++]=1;
for(int i=0;i<rec;++i)
{
printf("%d\n",ans[i]);
}
return 0;
}