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 lineN 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.
题意:有n个珠子,每个珠子左右两边有两种颜色,现在问这n个珠子能否连接成一条圆形项链,并输出珠子的链接顺序 ,要求每两个珠子相连必须是同色端。
题解:模板欧拉回路。
//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node << 1
#define rson mid + 1, r, node << 1 | 1
const int INF = 0x3f3f3f3f;
const int O = 1e5;
const int mod = 1e4 + 7;
const int maxn = 1e3+5;
const double PI = acos(-1.0);
const double E = 2.718281828459;
int f[maxn];
int col[maxn];
int way[51][51];
int n;
void init(){
MT(way, 0);
for(int i=1; i<=50; i++){
f[i] = i; col[i] = 0;
}
}
int find(int x){
return x == f[x] ? x : f[x] = find(f[x]);
}
void Union(int u, int v){
if((u=find(u))!=(v=find(v))){
f[u] = f[v];
}
}
void Oular(int u){ // 欧拉回路模板
for(int v=1; v<=50; v++) {
if(way[u][v]) {
way[u][v] -- ; way[v][u] -- ;
Oular(v);
printf("%d %d\n", v, u);
}
}
}
bool check(){ // 如果存在度为奇数,返回false, 如果不是连通图,返回false
int front = 0; //记录遍历的上一个节点
for(int i=1; i<=50; i++) {
if(col[i] & 1) return false;
if(!front && col[i]) {front = i; continue;}
if(col[i] && find(i) != find(front)) return false;
}
return true;
}
int main(){
int T, ca = 0; scanf("%d", &T);
while(T --) {
init(); scanf("%d", &n);
int st = 0; //起点
for(int i=0; i<n; i++) {
int u, v; scanf("%d%d", &u, &v);
if(!st) st = u;
way[u][v] ++; way[v][u] ++; // 记录每条边的的个数
col[u] ++; col[v] ++; // 记录每个节度的个数
Union(u, v);
}
printf("Case #%d\n", ++ca);
if(!check()) { // 判断是否存在欧拉回路
printf("some beads may be lost\n");
goto loop;
}
Oular(st);
loop:if( T ) printf("\n");
}
return 0;
}