http://poj.org/problem?id=2230
题意:给出n个field及m个连接field的边,然后要求遍历每条边仅且2次,求出一条路径来。
1 #include <stdio.h> 2 #include <string.h> 3 const int maxn = 10010; 4 const int maxm = 2*50005; 5 int head[maxn],vis[maxm]; 6 struct node 7 { 8 int u; 9 int v; 10 int next; 11 } edge[maxm]; 12 int n,m,cnt; 13 void init() 14 { 15 memset(head,-1,sizeof(head)); 16 memset(vis,0,sizeof(vis)); 17 cnt = 0; 18 } 19 void add(int u,int v) 20 { 21 edge[cnt].u = u; 22 edge[cnt].v = v; 23 edge[cnt].next = head[u]; 24 head[u] = cnt++; 25 } 26 void dfs(int u) 27 { 28 for (int i = head[u]; i!=-1; i = edge[i].next) 29 { 30 if (!vis[i]) 31 { 32 vis[i] = 1; 33 dfs(edge[i].v); 34 } 35 } 36 printf("%d ",u); 37 } 38 int main() 39 { 40 scanf("%d%d",&n,&m); 41 init(); 42 for (int i = 0; i < m; i ++) 43 { 44 int u,v; 45 scanf("%d%d",&u,&v); 46 add(u,v); 47 add(v,u); 48 } 49 dfs(1); 50 return 0; 51 }