Hiho 1182: Oralu San

Source: Internet
Author: User

1182: Oralu San

This is the tip of the topic:

Small ho: So, each time you turn an area, it is not the same as the original number minus the leftmost one, and at the end add 1 or 0.

So I think about "xyyy", it can turn into "YYY0" or "YYY1". I will treat all digital 0~2^n-1 as 2^n points, connecting all ("xyyy", "YYY0"), ("Xyyy", "YYY1").

For example, when n=3, I got a diagram like this:

All I have to do is find a path, start at one point, walk through all the points, and go back to the beginning. But I find it hard to look like.

Little hi: Of course. The path you have constructed is called the Hamiltonian loop, which is not so easy to solve.

Little ho: eh?? So what should I do?

Little hi: Actually your idea is no problem, but need to make a change. In your composition we use dots to represent numbers, so we need to go through every point. What if we use edges to represent every number?

Little ho: How do you use edges to represent numbers?

Small hi: In fact, it is very simple, for example, the number "10011", respectively, delete its first number and the last number, get "1001", "0011". Then we connect a forward edge from "1001" to "0011", representing the number "10011". Then we can get the method of composition:

For N, we construct a graph containing 2^ (N-1) points and 2^n edges, numbered from 0 to 2^ (N-1)-1. The point numbered I represents the number I. For any two points, if point I, point J satisfies the post n-2 number of point I and the first n-2 number of point J is the same, then we connect the forward edge (I,J). The Edge (I,J) represents the number ((I << 1) + (J & 1)). For example, when it comes to n=3, we can get:

It is easy to prove that for any of the different edges (i,j), the number represented must be different.

Small ho: This composition, as long as you find a European pull circuit on it. But will there be Euler circuits?

Small hi: Of course, for the direction of the map, the condition of the existence of the Euler road is that at most two points in the degree is not equal to the degree, and the two points to meet: one of the points in the degree of more than 1, another point out of the degree of 1 more than the degree.

If all points in the degree are equal to the degree, then there must be a Euler circuit. This can be proved by the same method as non-Tuoralo.

And we construct the diagram, by the construction method can know for any point, its penetration must be 2, the degree must be 2. So it must exist in the Euler loop.

The Fleury algorithm can still be used to find Oralu in a graph. Written in pseudo-code:

DFS (U): while (Start with U and not deleted Edge E (u,v)) Delete Edge E (u,v) DFS (v) endpathsize←pathsize + 1path[pathsize]←u

However, one thing to note is that when using the Fleury algorithm to calculate the Oralu of a graph, we need to reverse the path[] in order to get the correct path.

Little ho: What about finding the Euler circuit?

Small hi: After the Euler circuit is found, we can get the disk state of our goal as long as it is spliced on the euro-pull circuit.

Little ho: Well, I probably understand. I'm going to try it!

Topic Analysis:

The tip is clear: take a good look at the above explanation. The smart place is to turn the diagram into figure two (figure two, which means that the N-bit binary can represent all the numbers with the edge.)  N=3, is 0~7), saving space. If there is a Euler loop, that is, there is a path where each side passes and walks only once, and finally goes back to the origin, then it can be said that the turntable can represent all the numbers that the N-bit binary can represent. Use Fleury to find the path, edge of the ball path side record,Fleury do not understand point here .

#include <iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<math.h>#include<vector>using namespacestd;intN, top, key, final[40000], ans[40000];vector<int> vec[17000];voidInit ()//Initialize to mark all edges.{     for(inti =0; I < (1<< N-1); i++)    {        intj = i <<1;//point I move left one        intt =1<< (N-1); J= J & (T-1);//take the first place.Vec[i].push_back (j);//last one plus 0Vec[i].push_back (j+1);//last one Tim 1    }}voidDfsintx) {ans[++top] =x; if(Vec[x].size () >0)    {        intTMP = vec[x][0]; Vec[x].erase (Vec[x].begin ()+0);//Remember to delete the convenient pointsDFS (TMP); }}voidFleury (intx) {Top=1; Ans[top]=x;  while(Top >0)    {        if(Vec[ans[top]].size () >0)//if extensible, which route DFS can extend{Top--; DFS (Ans[top+1]); }        Else//the point x has no other edges that can go first (i.e. not expandable), then record it with final{final[++key] =Ans[top]; //ans Just records the intermediate records of the path, which may change, and make final records with final.top--; }    }}intMain () { while(SCANF ("%d", &n)! =EOF) {         for(inti =0; I < (1<< N); i++) vec[i].clear ();        Init (); Key=0; Fleury (0);  for(inti =1; I <= (1<< N); i++)//The last binary value of each value of the path is calculated        {            intTMP = Final[i] &1; printf ("%d", TMP); } cout<<Endl; }    return 0;}

Hiho 1182: Oralu San

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.