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

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;
}
时间: 2024-08-27 14:30:02

uva_10054_The Necklace(欧拉回路+打印路径)的相关文章

POJ 1041 John&#39;s trip 无向图的【欧拉回路】路径输出

欧拉回路第一题TVT 本题的一个小技巧在于: [建立一个存放点与边关系的邻接矩阵] 1.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为偶数 有向图: 欧拉回路:连通 + 所有点的入度 == 出度 欧拉路径:连通 + 源点 出度-入度=1 && 终点 入度 - 出度 = 1 && 其余点 入度 == 出度: 2.求欧拉路径 : step 1:选取起点(如果是点的度数全为偶数任意点为S如果有两个点的度数位奇数取一

hdu--1026--bfs&amp;&amp;优先队列&amp;&amp;打印路径

这题 是被我自己搞复杂了.... 太SB了.... 还是porker的关于输出路径的简洁 有效多了 touch  me #include <iostream> #include <cstring> #include <queue> #include <stack> using namespace std; int ans, n, m; const int size = 110; char maze[size][size]; bool vis[size][si

HDU 1385 Minimum Transport Cost(Floyd 最短路 打印路径)

HDU 1385 大意: 有N个城市,然后直接给出这些城市之间的邻接矩阵,矩阵中-1代表那两个城市无道路相连,其他值代表路径长度. 如果一辆汽车经过某个城市,必须要交一定的钱(可能是过路费). 现在要从a城到b城,花费为路径长度之和,再加上除起点与终点外所有城市的过路费之和. 求最小花费,如果有多条路经符合,则输出字典序最小的路径. 思路: Floyd求最短路,打印路径即可. 1 /*--------------------------------------------------------

CodeForces 10D. LCIS 最长公共上升子序列模板题 + 打印路径

推荐一篇炒鸡赞的blog. 下面代码中有打印路径. #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <cmath> #include <stack> #include <map> #include <

最长公共子序列Lcs(打印路径)

给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdkscab ab是两个串的子序列,abc也是,abca也是,其中abca是这两个字符串最长的子序列. Input 第1行:字符串A 第2行:字符串B (A,B的长度 <= 1000) Output 输出最长的子序列,如果有多个,随意输出1个. Input示例 abcicba abdkscab Output示例 abca #include <bits/stdc++.h> usin

uva 116 Unidirectional TSP【号码塔+打印路径】

主题: uva 116 Unidirectional TSP 意甲冠军:给定一个矩阵,当前格儿童值三个方向回格最小值和当前的和,就第一列的最小值并打印路径(同样则去字典序最小的). 分析:刚開始想错了,从前往后走,这种话没有办法控制字典序最小,用dfs标记了一下超时了. 事实上从后往前走就好了. 以后一定先想清楚顺序.然后dp的时候选择字典序最小的.用father数据记录就可以. AC代码: #include<iostream> #include<cstdio> #include&

0-1背包打印路径(递归和非递归版本)

简单的0-1背包打印路径问题,我们可以记录一个p[][]数组来判断,当前物品是否被选中,最后按照记录输出,注意是逆序. #include<stdio.h> #include<string.h> int main() { int a[25],p[25][10005],i,j,n,m,s[10005]; while(scanf("%d%d",&m,&n)!=EOF){ for(i=1;i<=n;i++) scanf("%d"

poj 2965 The Pilots Brothers&#39; refrigerator(dfs 枚举 +打印路径)

链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手,'+'代表关,'-'代表开,当16个把手都为开(即'-')时,门才能打开,问至少要几步门才能打开 改变状态规则:选定16个把手中的任意一个,可以改变其本身以及同行同列的状态(即若为开,则变为关,若为关,则变为开),这一次操作为一步. 分析:这题与poj 1753思路差不多,每个把手最多改变一次状态, 所有整个矩阵最多改变16次状态 思路:直接dfs枚举所有状态,直到找到目标状态 但是要打印路径,所有应在dfs时记录路径 注

Uva 642-CD(0-1背包+打印路径)

题目链接:点击打开链接 裸01背包 ,此题中 价值即体积... 打印路径..不多说了 一维的没看懂..上个二维的 #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <string> #include <cctype> #include <vector> #include <cstdio>