The Necklace UVA - 10054 题解(欧拉回路,路径输出)

来源:互联网 发布:android 开启网络权限 编辑:程序博客网 时间:2024/04/24 02:48

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 ≤ 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 ≤ N1, 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


一串项链每颗珠子两端有两种颜色,两颗珠子相邻端的颜色一样,然后项链断了,珠子撒了一地,问你收集起来的部分能不能构成一条项链(满足相邻端的颜色相同),问题变成了可以有多重边的图的欧拉回路了,判断连通和每个点的度来判断能不能构造欧拉回路,然后dfs输出路径。

#include<iostream>#include<string.h>#include<stdio.h>#include<string>#include<vector>#include<algorithm>using namespace std;int n;int f[55],du[55];bool is[55];int head[55], to[2050], nex[2050];bool used[2050];//int lnum = 0;int en;int F(int x){    if (f[x] == x)return x;    return f[x] = F(f[x]);}void add(int x,int y){    to[en] = y;    nex[en] = head[x];    head[x] = en++;}void dfs(int cnt){    int c=-1;    for (int i = head[cnt]; i != -1; i = nex[i]){        if (!used[i]){            c = to[i];            used[i] = 1; used[(i ^ 1)] = 1;            dfs(c);            printf("%d %d\n", c, cnt);        }    }}int main(){    int t;    int x, y;    scanf("%d", &t);    for (int cas = 1; cas <= t; cas++){        en = 0;        memset(used, 0, sizeof(used));        memset(is, 0, sizeof(is));        scanf("%d", &n);        for (int i = 1; i <= 50; i++){ f[i]=i; head[i] = -1; du[i] = 0; }        for (int i = 0; i < n; i++){            scanf("%d%d", &x, &y);            f[F(x)] = F(y);            du[x]++; du[y]++;            is[x] = 1; is[y] = 1;            add(x, y); add(y, x);        }        bool h_ans = 1;        int cnthead = -1;        for (int i = 1; i <= 50; i++){            if (is[i]){                if (cnthead == -1){ cnthead = F(i); }                else{                    if (F(i) != cnthead){ h_ans = 0; break; }                }                if (du[i] % 2 != 0){ h_ans = 0; break; }            }        }        if (cas !=1)printf("\n");        printf("Case #%d\n", cas);        if (h_ans == 0){ printf("some beads may be lost\n"); }        else{            dfs(cnthead);        }    }    return 0;}
0 0
原创粉丝点击