ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

UVA10054 UVALive2036 The Necklace【欧拉回路】

2019-03-10 09:43:05  阅读:414  来源: 互联网

标签:beads int UVALive2036 UVA10054 necklace test Necklace integer line


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 recolle ct 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 ≤ N ≤ 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 ≤ i ≤ 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

Regionals 2000 >> Asia - Shanghai

问题链接UVA10054 UVALive2036 The Necklace
问题简述:(略)
问题分析
    欧拉回路问题,输出欧拉回路。
    先统计出入度,然后进行欧拉回路的判定和处理。存在欧拉回路时则输出。回路输出用DFS实现。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10054 UVALive2036 The Necklace */

#include <bits/stdc++.h>

using namespace std;

const int N = 50;
const int M = 1000;
int degree[M + 1], g[M+ 1][M + 1];

void print(int u)
{
    for(int v = 1; v <= N; v++)
        if(g[u][v] > 0) {
            g[u][v]--;
            g[v][u]--;
            print(v);
            printf("%d %d\n", v, u);
        }
}

void output()
{
    for(int i = 1; i <= N; i++)
        if(degree[i]) {
            print(i);
            break;
        }
}

int main()
{
    int t, caseno = 0, n;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);

        memset(g, 0, sizeof(g));
        memset(degree, 0, sizeof(degree));

        // 读入边,统计各个结点间边数量,统计各个结点出入度(无向图)
        for(int i = 1; i <= n; i++) {
            int u, v;
            scanf("%d%d", &u, &v);
            g[u][v]++;
            g[v][u]++;
            degree[u]++;
            degree[v]++;
        }

        bool flag = false;
        for(int i = 1; i <= N; i++)
            if(degree[i] % 2) {
                flag = true;
                break;
            }

        printf("Case #%d\n", ++caseno);
        if(flag)
            printf("some beads may be lost\n");
        else
            output();
        printf("\n");
    }

    return 0;
}

标签:beads,int,UVALive2036,UVA10054,necklace,test,Necklace,integer,line
来源: https://www.cnblogs.com/tigerisland45/p/10504214.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有