sgu 101

http://acm.sgu.ru/problem.php?contest=0&problem=101

以0--6为顶点。骨牌为边。求欧拉通路。

注意:

1、连通

test 4是不连通的吧。

2、好像还有自环。

如果还wa的话。试试下面这组数据。可能是求欧拉通路的算法没理解好的原因。。我就是。。太弱。。

8
1 2
2 3
3 4
4 5
5 3
1 7
7 8
8 1
 1 // File Name: 101.cpp
 2 // Author: Missa
 3 // Created Time: 2013/2/23 星期六 1:22:12
 4 
 5 #include<iostream>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<algorithm>
 9 #include<cmath>
10 #include<queue>
11 #include<stack>
12 #include<string>
13 #include<vector>
14 #include<cstdlib>
15 #include<map>
16 #include<set>
17 using namespace std;
18 #define CL(x,v) memset(x,v,sizeof(x));
19 #define R(i,st,en) for(int i=st;i<en;i++)
20 
21 int n;
22 struct node
23 {
24     int v;
25     int id;//哪条边
26     char sy;//符号
27     node(){}
28     node(int v,int id,char sy):v(v),id(id),sy(sy){}
29 };
30 vector<node>vv[7];
31 vector<node>ans;
32 bool vis[105];
33 void dfs(int st)
34 {
35     R(i,0,vv[st].size())
36     {
37         node nt=vv[st][i];
38         //if(deg[nt.v]==1 && ans.size()<n-1)
39         //    continue;
40         if(vis[nt.id])
41             continue;
42         vis[nt.id]=1;
43         //deg[st]--;
44         //deg[nt.v]--;
45         dfs(nt.v);
46         ans.push_back(node(0,nt.id,nt.sy));//在dfs后面添加使得顺序逆的。如果在前面的话会使的上面的那种数据过不了。具体把上面那个数据画个图看看就知道了。搜索方向导致顺序错乱吧
47     }
48 }
49 int main()
50 {
51     while(~scanf("%d",&n))
52     {
53         CL(vv,0);
54         ans.clear();
55         int u,v;
56         R(i,1,n+1)
57         {
58             scanf("%d%d",&u,&v);
59             vv[u].push_back(node(v,i,'+'));
60             vv[v].push_back(node(u,i,'-'));
61         }
62         int cnt=0,st=-1;
63         R(i,0,7)
64         {
65             if(st==-1 && vv[i].size()>0)
66                 st=i;
67             if(vv[i].size()&1)
68             {
69                 cnt++;
70                 st=i;
71             }
72         }
73         if(cnt!=0 && cnt!=2)
74         {
75             puts("No solution");
76             continue;
77         }
78         CL(vis,0);
79         dfs(st);
80     //    cout<<ans.size()<<endl;
81         if(ans.size() != n)
82             puts("No solution");
83         else
84         {
85             for(int i=n-1;i>=0;i--)
86                 printf("%d %c\n",ans[i].id,ans[i].sy);
87         }
88     }
89     return 0; 
90 }

 

posted @ 2013-03-04 13:22  Missa  阅读(530)  评论(0编辑  收藏  举报