POJ 2230 (欧拉回路)

View Code
 1 /*
2 思路:
3 把这个无向图看成有向图
4 相当于无向图 的边走两边,同一条边走的方向不同
5 相当于求 有向图的 欧拉回路
6 */
7 #include<iostream>
8 #include<cstring>
9 using namespace std;
10 struct node
11 {
12 int b;
13 bool flag;
14 node *next;
15 }E[100010];
16 node table[10010];
17 int n,m;
18 void clur(int x)
19 {
20 node *p=table[x].next;
21 while(p)// 把用过的边标记一下,而不丢弃 (丢弃就运行不过 目前不知为什么)
22 {
23 if(p->flag==0)
24 {
25 p->flag=1;
26 clur(p->b);
27 cout<<x<<endl;
28 }
29 p=p->next;
30 }
31 }
32 int main()
33 {
34 int i,x,y,k;
35 while(cin>>n>>m)
36 {
37 k=0;
38 memset(table,0,sizeof(table));
39 for(i=1;i<=m;++i)
40 {
41 cin>>x>>y;
42 E[k].b=y;
43 E[k].next=table[x].next;
44 table[x].next=&E[k];
45 k++;
46
47 E[k].b=x;
48 E[k].next=table[y].next;
49 table[y].next=&E[k];
50 k++;
51 }
52 cout<<"1"<<endl;
53 clur(1);
54 }
55 system("pause");
56 return 0;
57 }

 

posted @ 2012-02-09 15:55  知行执行  阅读(184)  评论(0编辑  收藏  举报