poj.org/problem?id=2230
大意是给你一个无向图,要求找一条路径,走过每一条边恰好两次,且每次走的方向不同。很容易就能想到把无向图转化为有向图求欧拉回路。题目保证一定能找到从1点出发回到1点的答案。也就是说只需要深搜找到欧拉路径。
#include <vector>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int V = 10010;
const int E = 100010;
int n,m;
int cnt,head[V],path[V],top;
struct edge{
int u,v,next;
bool vis;
}node[E];
void Init(){
cnt = 0;
top = 1;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v){
node[cnt].v = v;
node[cnt].vis = 0;
node[cnt].next = head[u];
head[u] = cnt++;
}
void DFS(int u){
for(int i = head[u]; i!=-1; i=node[i].next){
if(!node[i].vis){
node[i].vis = true;
DFS(node[i].v);
cout << u << endl;
}
}
}
int main(){
scanf("%d%d",&n,&m);
Init();
int u,v;
for(int i=0; i<m; i++){
scanf("%d%d",&u,&v);
addedge(u, v);
addedge(v, u);
}
cout << "1" << endl;
DFS(1);
return 0;
}