UVA10054
来源:互联网 发布:java语言入门电子书 编辑:程序博客网 时间:2024/05/16 06:07
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 integerT.
The first line of each test case contains an integer N ( ) 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, printN lines with a single bead description on each line. Each bead description consists of two integers giving the colors of its two ends. For, the second integer on linei must be the same as the first integer on linei + 1. Additionally, the second integer on lineN 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
251 22 33 44 55 652 12 23 43 12 4
Sample Output
Case #1some beads may be lost Case #22 11 33 44 22 2
分析:
这题是考察无向图的欧拉回路,首先无向图要满足欧拉回路的:1.该图是连通的; 2.所有点的度数都是偶数;围绕这两点展开,首先判断题目给出的图是否构成一个欧拉回路,然后找到其中的一个欧拉回路即可。之前写的一个程序用的不是dfs的搜索,出了点问题,然后一直WA......但我还是没发现那种输出哪里不对
#include<cstdio>#include<vector>#include<string.h>#include<algorithm>using namespace std;typedef struct Edge{ int from, to; Edge(int from, int to){this->from = from; this->to = to;}}Edge;//定义一个结构体表示边const int maxn = 50 + 5;int G[maxn][maxn];int degree[maxn];vector<Edge> Brac;void Euler(int u)//输出欧拉回路{ for(int v = 1; v <= 50; v++){ if(G[u][v]){ G[u][v]--;G[v][u]--; Euler(v); Brac.push_back(Edge(u, v)); } }}int main(){ int T, N; scanf("%d", &T); for(int Case = 1; Case <= T; Case++){ int u, v; scanf("%d", &N); memset(G, 0, sizeof(G)); memset(degree, 0, sizeof(degree)); for(int i = 0; i < N; i++){ scanf("%d%d", &u, &v); G[v][u]++; G[u][v]++; degree[u]++; degree[v]++; }//首先将数据输入 int ok = 1; for(int i = 1; i <= 50; i++)//判断所有的点的度数是否为偶数 if(degree[i]%2){ ok = 0; break; } Brac.clear();//清空vector Euler(u);//找出欧拉回路 if(Brac.size() != N || Brac[0].to != Brac[Brac.size()-1].from) ok = 0;//构成手环需要满足Brac[0].to == Brac[Brac.size()-1].from //且Brac[i].to == Brac[i-1].from(1= <i <= Brac.size()-1) printf("Case #%d\n", Case); if(!ok) printf("some beads may be lost\n"); else for(int i = Brac.size()-1; i >= 0; i--) printf("%d %d\n", Brac[i].from, Brac[i].to); if(Case < T) printf("\n"); } return 0;}
- uva10054
- UVA10054
- uva10054
- UVA10054
- UVA10054
- UVa10054
- uva10054-The Necklace(项链)
- The Necklace UVA10054
- UVA10054 The Necklace
- UVa10054 The Necklace
- The Necklace UVA10054
- uva10054 The Necklace
- uva10054(欧拉
- 图论基础UVA10054
- UVA10054欧拉回路
- 例题5.3 项链 UVa10054
- UVA10054 The Necklace
- UVA10054-The Necklace
- reactiveCocoa学习
- js 身份证验证
- elasticsearch__6__es部分名词说明与配置文件详解
- 100 1的个数
- static作用(修饰函数、局部变量、全局变量)
- UVA10054
- 开发小细节汇总(持续更新)
- hdu-2306-改革春风吹满地
- Ninject-ioc随website部署到IIS上后,始终无法运行。
- C# Access的简单用法个人总结
- 网站QQ客服代码
- 福州化妆师培训哪里好?福州曼丹
- php在Windows下获取文件夹和大文件大小
- 在win8下安装tomcat8.0后,点击bin 目录下的启动文件startup.bat后,控制台一闪而过,根本启动不了tomcat