POJ 2230 Watchcow【欧拉回路】
题意: 在一个连通图中,经过每条边刚好两次,并且最终回到起点,找出这样的一条路径。
分析: 在每条边基础上加多一条不同方向的边,dfs所有边,找出欧拉回路。
#include<stdio.h> #include<string.h> struct node { int from,to,next; }e[100005]; int head[10005]; int tot; void add(int s,int u) { e[tot].from=s; e[tot].to=u; e[tot].next=head[s]; head[s]=tot++; } int v[100005]; int q[100005]; int n,m,top; void dfs(int s) { int i; for(i=head[s];i;i=e[i].next) { if(!v[i]) { v[i]=1; dfs(e[i].to); q[top++]=i; } } } int main() { int a,b,i; scanf("%d%d",&n,&m); tot=1; memset(head,0,sizeof(head)); memset(v,0,sizeof(v)); while(m--) { scanf("%d%d",&a,&b); add(a,b); add(b,a); } top=1; dfs(1); top--; printf("%d\n%d\n",e[q[top]].from,e[q[top]].to); for(i=top-1;i>=1;i--) printf("%d\n",e[q[i]].to); return 0; }