• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
DementRock
   首页          管理     

Sgu 101

将条件转换为一个图,0~6作为图中的顶点,每一张多米诺骨牌作为一条连接两点数的无向边,那么答案就是一条欧拉路径。

判定方法:一个图中存在欧拉路径,当且仅当这个图连通(此题中无需考虑),并且奇点数(即度数为奇数的顶点数)为0或者2。

若奇点数为0,则从任意一个顶点开始做一次dfs即可。

若奇点数为2,则从其中一个奇点开始做一次dfs即可。

注意:

1、不要忘了顶点0!

2、生成路径后要判断是否连通!

3、判断是否经过每一条边!

sgu101
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int n,deg[10],linker[10][10],
 4 anslist[200][2],ansnum,a[200][2],
 5 slinker[10][10],listall[10][10][200],
 6 ansout[200][2];
 7 void init()
 8 {
 9     int x,y;
10     scanf("%d",&n);
11     for(int i=1;i<=n;++i){
12         scanf("%d%d",&x,&y);
13         a[i][0]=x;
14         a[i][1]=y;
15         ++deg[x];
16         ++deg[y];
17         ++linker[x][y];
18         ++linker[y][x];
19         listall[x][y][++listall[x][y][0]]=i;
20     }
21 }
22 void dfs(int now)
23 {
24     for(int i=0;i<=6;++i)
25         if(linker[now][i])
26         {
27             --linker[now][i];
28             --linker[i][now];
29             dfs(i);
30             anslist[++ansnum][0]=now;
31             anslist[ansnum][1]=i;
32         }
33 }
34 void work()
35 {
36     int tmp=0,start;
37     for(int i=0;i<=6;++i)
38         if(deg[i]>0)
39         {
40             start=i;
41             break;
42         }
43     for(int i=0;i<=6;++i)
44         if(deg[i]&1)
45         {
46             start=i;
47             ++tmp;
48         }
49     if(tmp!=0&&tmp!=2)
50     {
51         printf("No solution\n");
52         exit(0);
53     }
54     dfs(start);
55     if(ansnum<n)
56     {
57         printf("No solution\n");
58         exit(0);
59     }
60 }
61 void print()
62 {
63     for(int i=ansnum;i>=1;--i)
64     {
65         int x=anslist[i][0];
66         int y=anslist[i][1];
67         if(i<ansnum&&x!=anslist[i+1][1])
68         {
69             printf("No solution\n");
70             exit(0);
71         }
72         if(listall[x][y][0])
73         {
74             ansout[ansnum-i+1][0]=listall[x][y][listall[x][y][0]];
75             ansout[ansnum-i+1][1]=1;
76             --listall[x][y][0];
77         }
78         else if(listall[y][x][0])
79         {
80             ansout[ansnum-i+1][0]=listall[y][x][listall[y][x][0]];
81             ansout[ansnum-i+1][1]=0;
82             --listall[y][x][0];
83         }
84         else
85         {
86             printf("No solution\n");
87             exit(0);
88         }
89     }
90     for(int i=1;i<=ansnum;++i)
91         printf("%d %c\n",ansout[i][0],ansout[i][1]?'+':'-');
92 }
93 int main()
94 {
95     init();
96     work();
97     print();
98     return 0;
99 }


 

posted @ 2010-02-16 22:37  DementRock  阅读(1208)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3