UVA 10054.The Necklace(递归求欧拉回路)

来源:互联网 发布:淘宝steam充值卡知乎 编辑:程序博客网 时间:2024/06/05 14:32

题目大意:给你n颗珠子,每颗上面会有两种颜色,需要你把相同的颜色对接在一起,最终把所有珠子都用上,相连成为一条回路(项链)。如果可以输出项链的样子;反之,输出“some beads may be lost"。

Description

Download as PDF


  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 \leN \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 

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



Miguel Revilla
2000-12-28

简单粗暴的一道题,其实就是DFS找到并输出一条欧拉回路(所有边用且仅用过一次),但缺点是此题数据比较水,没有不连通的情况,不过我在AC代码中还是加上这一判断了。

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

证明连通性:每个结点入度等于出度。

#include <iostream>#include <cstdio>#include <cstring>#include <vector>using namespace std;int t, n, a, b;int map[55][55], point[55];struct node {    int x, y;    node(int x, int y) : x(x), y(y) {}};vector<node> vec;void DFS(int x) {    for(int i = 0; i <= 50; i++) {        if(map[x][i] > 0) {            map[x][i]--; map[i][x]--;            point[i]--; point[x]--;//每用过一条边就给减去用过的入度和出度            DFS(i);            vec.push_back(node(x, i));        }    }}bool judge() {//判断所有点是否连通    for(int i = 1; i <= n; i++){        if(point[i] != 0) return false;    }    return true;}int main() {    while(~scanf("%d", &t)) {        for(int count = 1; count <= t; count++) {            scanf("%d", &n);            vec.clear();            memset(map, 0, sizeof(map));            memset(point, 0, sizeof(point));            for(int i = 0; i < n; i++) {                scanf("%d%d", &a, &b);                point[a]++; point[b]++;                map[a][b]++; map[b][a]++;            }            printf("Case #%d\n", count);            for(int i = 1; i <= 50; i++)                if(point[i] & 1) {                    printf("some beads may be lost\n");                    goto loop;                }            DFS(a);            if(judge()) {                for(int i = vec.size() - 1; i >= 0; i--)                    printf("%d %d\n", vec[i].x, vec[i].y);            } else printf("some beads may be lost\n");            loop: ;            if(count != t) printf("\n");        }    }    return 0;}


0 0
原创粉丝点击