Uva10054 the Necklace

Source: Internet
Author: User

Uva10054 Euler circuit

Test instructions: give n beads, each of the two ends of the beads are different colors. Ask whether a necklace can be formed, the two adjacent beads adjacent to the opposite ends of the same color.

Idea: think of color as a point. Beads are seen as edges. Each bead is connected between the two colors. The number of degrees for any point in the no-map is even, and the graph has a Euler loop. You can then use the Euler function to output it.

/*id:onlyazh1lang:c++task:the Necklace*/#include<iostream>#include<cmath>#include<stack>#include<queue>#include<cctype>#include<vector>#include<cstring>#include<fstream>#include<iomanip>#include<algorithm>using namespacestd;typedef unsignedLong Longll;intdeg[ -];intg[ -][ -];voidEulerintu) {     for(intI=1; i<= -; i++)        if(G[u][i]) {G[u][i]--; g[i][u]--;            Euler (i); cout<<i<<" "<<u<<Endl; }}intMain () {//ifstream cin ("In.txt");    intT,n,u,v,icase=0; CIN>>u;  while(t--) {memset (deg,0,sizeof(deg)); memset (G,0,sizeof(G)); CIN>>N;  while(n--) {cin>>u>>v; G[U][V]++; g[v][u]++; Deg[u]+ +;d eg[v]++; }        if(icase) cout<<Endl; cout<<"Case #"<<++icase<<Endl; BOOLflag=true;  for(intI=1; i<= -; i++)            if(deg[i]%2==1) {cout<<"Some beads may lost"<<Endl; Flag=false; Break; }        if(flag) Euler (U); }    return 0;}
View Code

Uva10054 the Necklace

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.