uva 10054 - The Necklace

  Problem D: The Necklace 

My little sister had a beautiful necklace made of colorful beads. Two successive beads in the necklace shared a common color at their meeting point. The figure below shows a segment of the necklace:

But, alas! One day, the necklace was torn and the beads were all scattered over the floor. My sister did her best to recollect all the beads from the floor, but she is not sure whether she was able to collect all of them. Now, she has come to me for help. She wants to know whether it is possible to make a necklace using all the beads she has in the same way her original necklace was made and if so in which order the bids must be put.

Please help me write a program to solve the problem.

Input 

The input contains T test cases. The first line of the input contains the integer T.

The first line of each test case contains an integer N ( $5 \le
N \le 1000$) giving the number of beads my sister was able to collect. Each of the next N lines contains two integers describing the colors of a bead. Colors are represented by integers ranging from 1 to 50.

Output 

For each test case in the input first output the test case number as shown in the sample output. Then if you apprehend that some beads may be lost just print the sentence ``some beads may be lost" on a line by itself. Otherwise, print N lines with a single bead description on each line. Each bead description consists of two integers giving the colors of its two ends. For $1 \le i \le N ­ 1$, the second integer on line i must be the same as the first integer on line i + 1. Additionally, the second integer on line N must be equal to the first integer on line 1. Since there are many solutions, any one of them is acceptable.

Print a blank line between two successive test cases.

Sample Input 

2
5
1 2
2 3
3 4
4 5
5 6
5
2 1
2 2
3 4
3 1
2 4

Sample Output 

Case #1
some beads may be lost
 
Case #2
2 1
1 3
3 4
4 2
2 2
有一堆散落的项链的的珠子,珠子有可能重复的出现,问我们能否连接成一条项链,要求该项链的每一节的两个珠子要满足;
找了些欧拉回路的相关资料

对无向图:  

定义:给定无孤立结点图G,若存在一条路,经过图中每条边一次且仅仅一次,该条路称欧拉路,若存在一条回路,经过图中每边一次且仅仅一次,该回路称为欧拉回路。具有欧拉回路的图称为欧拉图,不是柏拉图。

定理:无向图G具有一条欧拉路,当且仅当G是连通的,且有0个或者是两个奇数度得结点。

推论:无向图G具有一条欧拉回路,当且仅当G是连通的,并且所有结点的度数均为偶数。

一笔画问题就是典型的这类问题:要判定一个图G是否可一笔画出,有两种情况, 从图中某一个结点出发,经过图G中每个边一次再回到该结点,或者是从G中某一个结点出发,经过G中每边一次且仅一次到达另一个结点,分别对应着欧拉回路和欧拉路的问题

 对有向图:

定义:给定有向图G,通过图中每边一次且仅一次的一条单向路(回路),称作单向欧拉路(回路)。

定理:有向图G具有 单向欧拉路,当且仅当它是连通的,而且除两个结点外,每个结点的入度等于出度,但这两个结点中,一个结点的入度比出度大1,另一个结点的入度比出度小1。 

定理:有向图G具有一条单向欧拉回路,当且仅当是连通的,且每个结点入度等于出度。 

以及Fleury算法:
    (1)任取v0∈V(G),令P0=v0;
    (2)设Pi=v0e1v1e2...eivi已经行遍,按下面方法来从E(G)-{e1,e2,...,ei}中选
         取ei+1:
        (a)ei+1与vi想关联;
        (b)除非无别的边可供行遍,否则ei+1不应该为Gi=G-{e1,e2,...,ei}中的桥.
    (3)当(2)不能再进行时,算法停止。
例如数据:用电脑把每一步输出,大致理解就是随便找个点然后一它为起点开始遍历,到不能继续时,返回上一层,这是侯输出刚才无法继续遍历的那个店,也就是以他为起点,利用dfs的递归返回时的过程输出,这个算法比回溯快些,
15 2 1 2 2 3 4 3 1 2 4 2 1 2 2 3 4 3 1 2 4 2 1 2 2 3 4 3 1 2 4 Case #1 deep=1 [1 2] deep=2 [2 1] deep=3 [1 2] deep=4 [3 1] deep=5 [1 3] deep=6 [3 1] deep=7 [4 3] deep=8 [2 4] deep=9 [2 2] deep=10 [2 2] deep=11 [2 2] deep=12 [4 2] deep=13 [2 4] deep=13 2 4 deep=13 [3 4] deep=14 [4 3] deep=14 4 3 deep=13 3 4 deep=12 4 2 deep=11 2 2 deep=10 2 2 deep=9 2 2 deep=8 2 4 deep=7 4 3 deep=6 3 1 deep=5 1 3 deep=4 3 1 deep=3 1 2 deep=2 2 1 deep=1 1 2
这个题目存在重边问题,其实重边按一条边算,判断连通性是一样的,做的时候忘记加判断了,ac了数据弱了。
#include<stdio.h>
int n,map[60][60];
void euler(int m)
{int i;
 for (i=1;i<=50;i++)
 if (map[m][i]>0)
 {--map[m][i];  --map[i][m];
  euler(i);
  printf("%d %d\n",i,m);
 }
}
int main()
{int t,f,x,y,i,j,k;
 scanf("%d",&t);
 for (k=1;k<=t;k++)
 {
  for (i=0;i<=50;i++)
  for (j=0;j<=50;j++)
  map[i][j]=0;
  f=0;
  scanf("%d",&n);
  for (i=1;i<=n;i++)
  {scanf("%d%d",&x,&y);
   ++map[x][0]; ++map[y][0];
   ++map[x][y]; ++map[y][x];
  }
  for (i=1;i<=50;i++)
  {f=map[i][0]%2;
   if (f) break;
  }
  printf("Case #%d\n",k);
  if (f) printf("some beads may be lost\n");
    else euler(x);
  if (k<t) printf("\n");
 }
 return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值