Test instructions
Sister has a necklace, this necklace is composed of many beads, beads are colored, the intersection of the two continuous beads color is the same, that is, for the adjacent two beads, the end of the previous bead color and the next bead of the first end of the same color. One day, the necklace broke, beads spilled a place, everywhere, my sister put all the way to the floor can see the beads (5-1000) are picked up, but not sure whether the collection of Qi. Give you the color number (1-50) of the ends of the beads collected by his sister, so that you decide whether or not to collect them.
Ideas:
Consider the color as a point, a bead as a non-edge, then the problem has a solution, when and only if there is a Euler circuit in the diagram. So first of all to determine whether the test instructions constructed by the non-direction of the existence of the Euler circuit, the non-map can be built out Eulerian circuit needs to meet two conditions:
1: The Basemap is connected, you can use and check the set or DFS judge, here use and check set.
2: There are no points with an odd number of degrees.
After the judgment is completed, the whole graph is traversed by DFS, and each access point is pressed into the stack, and the current point is bounced back and recorded. Then the order of the points in the Euler loop is obtained, and the successive two points are the edges that the problem needs to be output.
Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7#include <stack>8#include <queue>9 using namespacestd;Ten One Const intMAXV = -; A Const intMaxe = +; - intDEGREE[MAXV +7]; - intPRE[MAXV +7]; the intHEAD[MAXV +7]; - intVis[maxe *2+7]; - intE, V; - + structEdgenode - { + intto ; A intNext; at}edges[2* Maxe +7]; - - voidInitpre () - { - for(inti =1; I <= MAXV; i++) Pre[i] =i; - } in - intFind (intx) to { + returnx = = Pre[x]? X:PRE[X] =Find (pre[x]); - } the * voidMixintXinty) $ {Panax Notoginseng intFX =Find (x); - intFY =Find (y); the if(FX! = FY) Pre[fx] =fy; + } A the intIseuler () + { - for(inti =1; I <= MAXV; i++) $ if(Degree[i] &1)return 0; $ return 1; - } - the intisconnct () - {Wuyi intCNT =0; the for(inti =1; I <= MAXV; i++) - if(Degree[i] && pre[i] = = i) cnt++; Wu if(CNT = =1)return 1; - return 0; About } $ -stack<int>EU; - intAns[maxe +7]; - intLen; A voidEulerdfs (intNow ) + { the Eu.push (now); - for(intk = Head[now]; K! =-1; K =edges[k].next) $ { the if(!Vis[k]) the { theVIS[K] =1; the if(K &1) Vis[k +1] =1; - ElseVis[k-1] =1; in Eulerdfs (edges[k].to); the } the } AboutAns[++len] = Eu.top ();//storing the sequence of Euler loop points the Eu.pop (); the } the + intMain () - { theFreopen ("In.txt","R", stdin);Bayi intT; thescanf"%d", &T); the intKas =0; - while(t--) - { thescanf"%d", &E); thememset (Degree,0,sizeof(degree)); thememset (Edges,0,sizeof(Edgenode)); theMemset (Head,-1,sizeof(head)); - Initpre (); the intSt =Wuyi;//Default Path Start the for(inti =1; I <= E; i++) the {94 intu, v; thescanf"%d%d", &u, &v); theedges[2I1].to = v;//chain forward star Storage non-directional graph side need to be saved one time theedges[2I1].next =Head[u];98Head[u] =2I1; Aboutedges[2* I].to =u; -edges[2* I].next =Head[v];101HEAD[V] =2*i;102degree[u]++;103degree[v]++;104 Mix (U, v); theSt =min (St, min (U, v));106 }107printf"Case #%d\n", ++Kas);108 if(Isconnct () &&Iseuler ())109 { thememset (Vis,0,sizeof(Vis));111memset (ans,0,sizeof(ans)); theLen =0;113 Eulerdfs (ST); the for(inti =1; i < Len; i++)//Two adjacent points form an edge theprintf"%d%d\n", Ans[i], ans[i +1]); the }117 Else118printf"some beads may lost\n");119 if(T) printf ("\ n"); - }121 return 0;122}
(PS: Not only test instructions not understand or what, if the sister did not pick up just also constitute the Euler circuit, then it is still not collected Qi, but so string up should be found (escape ...)
UVA 10054 The necklace (Euler loop without direction graph)