码迷,mamicode.com
首页 > 其他好文 > 详细

uva_10054_The Necklace(欧拉回路+打印路径)

时间:2015-04-29 21:52:47      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:acm   algorithm   uva   欧拉回路   

10054  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 ≤ 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


题意:给你多个珠子,每个珠子有两种颜色。问你能否把它们串成一个圈,使得每两个相邻的颜色相同。

分析:欧拉回路。每个珠子的两种颜色为两个点,之间连一条边,求解该无向图的欧拉回路。先判断,后输出路径。

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18806

代码清单:

#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<cctype>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxv = 50 + 5;
const int maxn = 2000 +5;  //一开始用的1005的数组,re了好几发。后来才想到无向图。。。

struct Edge{
    int u,v;
    int pos;
};

int number,pose;
int T,n,a,b,post;
int pose1,pose2;
int father[maxv];
int degree[maxv];
bool used[maxv];      //记录哪些点出现
bool vis[maxn];       //寻找路径时对边进行标记
Edge path[maxn];     //记录路径
vector<Edge>G[maxv];  //存储信息

//初始化
void init(){
    pose=0;
    number=0;
    memset(vis,false,sizeof(vis));
    memset(used,false,sizeof(used));
    memset(degree,0,sizeof(degree));
    for(int i=0;i<maxv;i++) father[i]=i;
    for(int i=0;i<maxv;i++) G[i].clear();
}

//加边
void Add(int a,int b){
    Edge an;
    an.u=a; an.v=b; an.pos=number++;
    G[a].push_back(an);
    an.u=b; an.v=a; an.pos=number++;
    G[b].push_back(an);
}

//并查集判断连通性
int Find(int x){
    if(x!=father[x]) return Find(father[x]);
    return father[x];
}

//判断是否是欧拉回路:图连通,所有点度数为偶数
bool isEuler(){
    int cost=0;
    for(int i=1;i<=50;i++){
        if(!used[i]) continue;
        if(father[i]==i) cost++;
        if(degree[i]&1) return false;
    }
    if(cost>1) return false;
    return true;
}

//寻找欧拉路径
void dfs(int x){
    for(int i=0;i<G[x].size();i++){
        Edge& e=G[x][i];
        if(!vis[e.pos]){
            if(e.pos&1) { vis[e.pos]=vis[e.pos-1]=true; }
            else { vis[e.pos]=vis[e.pos+1]=true; }
            dfs(e.v);
            path[++pose]=e;
        }
    }
}

//打印欧拉路径
void print(int x){
    dfs(x);
    for(int i=pose;i>0;i--)
        printf("%d %d\n",path[i].u,path[i].v);
}

int main(){
    scanf("%d",&T);
    for(int k=1;k<=T;k++){
        init();
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d%d",&a,&b);
            post=a;
            degree[a]++;
            degree[b]++;
            Add(a,b);
            pose1=Find(a);
            pose2=Find(b);
            father[pose2]=pose1;
            used[a]=used[b]=true;
        }
        if(k>1) printf("\n");
        printf("Case #%d\n",k);

        if(!isEuler())
            printf("some beads may be lost\n");
        else
            print(post);
    }return 0;
}

额,再yy一下。其实刚开始写的不是这个代码,刚入手的时候是用map处理掉 数字使得它们从1~cnt 是连续的,这样子自己认为好用一点;但是无疑这也浪费了时间和空间。而且最后打印出路径必须还原成之前的数字(truly[i])。

挫代码清单:

#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<cctype>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxv = 50 + 5;
const int maxn = 2000 +5;

struct Edge{
    int u,v;
    int pos;
};

int number,pose;
int T,n,cnt,a,b;
int father[maxv];
int degree[maxv];
int truly[maxv];      //存储map处理的点的原点,为了打印路径
bool vis[maxn];       //标记路径
Edge path[maxn];      //存储路径
map<int,int>m;        //使得点处理成1~cnt是连续的
vector<Edge>G[maxv];  //存储信息,边

//初始化
void init(){
    cnt=0;
    pose=0;
    number=0;
    m.clear();
    memset(vis,false,sizeof(vis));
    memset(path,0,sizeof(path));
    memset(truly,0,sizeof(truly));
    memset(degree,0,sizeof(degree));
    for(int i=0;i<maxv;i++) father[i]=i;
    for(int i=0;i<maxv;i++) G[i].clear();
}

//加边
void Add(int a,int b){
    Edge an;
    an.u=a; an.v=b; an.pos=number++;
    G[a].push_back(an);
    an.u=b; an.v=a; an.pos=number++;
    G[b].push_back(an);
}

//并查集判连通性
int Find(int x){
    if(x!=father[x]) return Find(father[x]);
    return father[x];
}

//欧拉回路的判断
bool isEuler(){
    int cost=0;
    for(int i=1;i<=cnt;i++){
        if(father[i]==i) cost++;
        if(degree[i]&1) return false;
    }
    if(cost>1) return false;
    return true;
}

//求解欧拉路径
void dfs(int x){
    for(int i=0;i<G[x].size();i++){
        Edge& e=G[x][i];
        if(!vis[e.pos]){
            if(e.pos&1) { vis[e.pos]=vis[e.pos-1]=true; }
            else { vis[e.pos]=vis[e.pos+1]=true; }
            dfs(e.v);
            path[++pose]=e;
        }
    }
}

//打印欧拉路径
void print(){
    dfs(1);
    for(int i=pose;i>0;i--)
        printf("%d %d\n",truly[path[i].u],truly[path[i].v]);
}

int main(){
    scanf("%d",&T);
    for(int k=1;k<=T;k++){
        init();
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d%d",&a,&b);
            if(!m.count(a)) m[a]=++cnt;
            if(!m.count(b)) m[b]=++cnt;
            int pose1=m[a];
            int pose2=m[b];
            truly[pose1]=a;
            truly[pose2]=b;
            degree[pose1]++;
            degree[pose2]++;
            Add(pose1,pose2);
            pose1=Find(pose1);
            pose2=Find(pose2);
            father[pose2]=Find(pose1);
        }
        if(k>1) printf("\n");
        printf("Case #%d\n",k);

        if(!isEuler())
            printf("some beads may be lost\n");
        else
            print();
    }return 0;
}


uva_10054_The Necklace(欧拉回路+打印路径)

标签:acm   algorithm   uva   欧拉回路   

原文地址:http://blog.csdn.net/jhgkjhg_ugtdk77/article/details/45370549

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!