Uva_10054_the Necklace (Euler circuit + print path)

Source: Internet
Author: User
Tags in degrees cmath

10054 the Necklace

My Little sister had a beautiful necklace made of colorful beads. Successive beads in the necklace gkfx a common color at their meeting point. The figure below shows a segment of the necklace:

But, alas! One day, the necklace is torn and the beads were all scattered on the floor. My sister did his best to recollect all the beads from the floor, but she isn't sure whether she was able to collect all of them. Now, she had come to me for help. She wants to know whether it's possible to make a necklace using all the beads she's have in the same the it's her original Neckl ACE is made and if so in which order the bids must is 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 all test case contains an integer N (5≤n≤1000) giving the number of beads my sister is able to col Lect. Each of the next N lines contains and integers describing the colors of a bead. Colors is represented by integers ranging from 1 to 50.

Output

For each test, the input first output the test case number as shown in the sample output. Then if you apprehend this some beads may be lost just print the sentence "Some beads is lost" on a line by itself. Otherwise, print N lines with a single bead description on each line. Each bead description consists of both integers giving the colors of its and ends. For 1≤i≤n 1, the second integer in line I must is the same as the first integer on line i + 1. Additionally, the second integer on line N must is equal to the first integer in line 1. Since There is many solutions, any one of the them is acceptable.

Print a blank line between successive test cases.

Sample Input

2

5

1 2

2 3

3 4

4 5

3 p

5

2 1

2 2

3 4

3 1

2 4


Sample Output

Case #1

Some beads may lost


Case #2

2 1

1 3

3 4

4 2

2 2


Test instructions: give you multiple beads, each bead has two colors. Ask if you can string them into a circle, making each of the two adjacent colors the same.

Analysis: Euro-pull circuit. The two colors of each bead are two points, and a single edge is drawn between them to solve the Euler loop of the non-direction graph. First judge, after the output path.

Title Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18806

Code Listing:

#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 = + 5;const int maxn = 2000 +5; The first 1005 of the array, re several rounds. Then I thought about the graph of no-show ...    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];       Record which points appear bool VIS[MAXN];     Mark Edge PATH[MAXN When looking for a path];  Record Path vector<edge>g[maxv];    Store information//Initialize 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 ();}    Add edge 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);} And check the set to determine the connectivity of int FIND (int x) {if (x!=father[x]) return Find (Father[x]); return father[x];}    Determine if it is a Euler circuit: graph connectivity, all points in degrees of even 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;}        Look for the Euler path 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;    }}}//print Euler path 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;}

Well, then yy. In fact, just started writing is not this code, just start with the map to deal with the numbers so that they from the 1~cnt is continuous, this looks like it is easy to use a little, but undoubtedly this also wasted time and space. And the last print out path must be restored to the previous number (Truly[i]).

Code Listing:

#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 = + 5;const int maxn = +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];       Stores the origin point of the map processing point, in order to print the path bool VIS[MAXN];      Mark Path Edge PATH[MAXN];        Storage Path map<int,int>m;  So that the point processing into 1~cnt is continuous vector<edge>g[maxv];    Storage information, EDGE//Initialization of 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 ();}    Add edge 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);}    And check the set of connectivity int find (int x) {if (x!=father[x]) return find (Father[x]); return father[x];}    Euler circuit judgment 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;}        Solve Euler path 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;    }}}//print Euler path 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 (Euler circuit + print path)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.