Hiho 50th Zhou Shi

Source: Internet
Author: User

Topic Link: Click here~~

"The main topic"

time limit:10000ms single point time limit:1000ms memory limit:256MB
Description

In the last back small hi and small ho control the protagonist gathered scattered on the wooden bridge props, these props are actually a piece of dominoes.

The protagonist continued to move forward, a stone bridge appeared in front of the stone bridge at the end of a fire wall, it seems unable to pass.

Little Hi noticed that there was a small piece of paper at the bridge, so the controlling protagonist picked up the paper, and saw it read:

The flame wall can be closed by placing the M-block dominoes in the concave of the stone bridge. Remember that dominoes need the same number to connect. --by unknown adventurer.

Small hi and Small ho opened the main character of the props bar, found that the protagonist happens to have M fast Domino.

Small ho: That means to put all the dominoes in the groove to close the flame wall, what does the number mean?

Little hi: You see, each piece of dominoes has a number at each end, presumably only when the number is the same, can be placed together, such as:

Little ho: So, let's see if we can connect all the dominoes together.

 

Hint: Fleury algorithm to find Euler path

  input

Line 1th: 2 positive integers, n,m. Indicates the maximum number and number of dominoes that appear on the dominoes, respectively. 1≤n≤1,000,1≤m≤5,000

2nd.. M+1 lines: 2 integers per line, u,v. Line I+1 represents the number (U,V) at both ends of the block I, 1≤u,v≤n

Output

Line 1th: m+1 numbers, indicating the number after the end of a domino

For example, the status of the Domino connection is (1,5) (5,3) (3,2) (2,4) (4,3), then output "1 5 3 2 4 3"

You can output any set of legitimate solutions.

Sample input
5 53 53 24 23 45 1
Sample output
1 5 3 4 2 3
"ideas":

hint: Fleury algorithm to find Euler path

Little ho: Just give me this simple puzzle!

Little hi: Really, no problem?

<10 minutes past >

Little ho: Ah ah ah ah ah! Can't do it!!! The number of dominoes is a lot of chaos.

Little hi: Hey, I knew you were going to have a problem.

Little ho: Little hi come help me!

Little hi: Okay, all right. Let's solve the problem together.

< little hi Think about >

Little hi: It turns out that ... Little Ho You look closely at this example:

Since the two connected numbers are always the same, we can write them only once, so this example could be written as: 3-2-4-3-5-1. 6 digits have exactly 5 gaps, and the numbers on both sides of each gap correspond exactly to a single domino.

If we treat each number as a point, each piece of dominoes is considered an edge. What do you think of it?

Small ho: In this case, it is:

To connect all the dominoes, that is, to walk all the edges once. Hey, this is not Euler road problem!

Little hi: Yes, the problem is actually a problem with the Euler road, but unlike the last time, this time we're going to find a way to get to the European path.

Little ho: So how do we find a way?

Little hi: Let's just borrow the last example.

Using our last method of proving Euler's path, we found 2 paths in this example:

L1:4-5-2-3-6-5l2:2-4-1-2

Suppose we stack s, which records the order of the nodes each time we look for a path. When we find L1, the situation in stack S is:

S:4 5 2 3 6 5 [Top]

At this point we step out of the stack and delete the edges. When we go to Node 2 o'clock, we find that Node 2 is exactly the public node of L1 and L2. And L2 satisfies the other side after passing back to Node 2. If we take L2 in this place first, and then continue to walk L1 not just go through all sides.

And in the last proof we know that in addition to L1, other paths L2, L3 ... Must be satisfied with the starting point and the endpoint. So starting from any common node there must be a path back to this node.

Thus we get an algorithm:

    1. Find a L1 path in the original image

    2. Backtrack from the end of the L1, and then stack each point in turn. And check if there are any other edges that are not passing through the current point. If there is a current point as the starting point, find L2, and the L2 node is also used to repeat the stack record of the algorithm.

    3. When the points in the L1 are all out of the stack, the algorithm ends.

Here we have another 3-story example:

In this example:

L1:1-2-6-5-1l2:2-3-7-2l3:3-4-8-3

In the first step we L1 the stack s, and we use an array path to record the order of our stacks:

S: [1 2 6 5 1]path:

And then out of the stack to Node 2 o'clock we found 2 other paths, so we added another path of 2:

s:1 [2 3 7 2]path:1 5 6

At this point the L2 has finished, and then starts popping the element until we find that 3 has another path, which is also pressed into the stack:

The remaining elements are then popped up in turn:

S:path:1 5 6 2 7 3 8 4 3 2 1

The path at this point is exactly the Euler path we need.

Little ho: It was ingenious to find Euler's way.

Little hi: And this algorithm has a very clever way to implement it. Because DFS itself is a process to stack out, so we directly use the nature of Dfs to implement the stack, the pseudo-code is as follows:

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

Little ho: This code is so simple, I think I can achieve it!

Little hi: Then the realization is yours.

Little ho: No problem! Give it to me.

Code:

#include <bits/stdc++.h>using namespace Std;const int n=5050;int m[n][n];int v,u,n,m,k,cnt,ans;int Path[N], pathsize;void DFS (int u) {path[pathsize++]=u;//into stack for (int i=1; i<=n; i++) {if (M[u][i])//delete this edge, note that the problem may have a heavy edge            , the value of 0 will wa {m[u][i]--;            m[i][u]--;            DFS (i);        Break    }}}void Fleury (int x) {int flag;    pathsize=0;    Path[pathsize++]=x;        while (pathsize>0) {flag=true; for (int i=1; i<=n; i++) {if (M[path[pathsize-1]][i])//try to search for an edge other than cut Edge (BRIDGE) {FLA                G=false;            Break            }} if (flag)//If no point can be extended, output and stack {if (pathsize!=1) printf ("%d", path[--pathsize]);        else printf ("%d", path[--pathsize]);        } else {DFS (path[--pathsize]); }} puts ("");}    int main () {scanf ("%d%d", &n,&m);    memset (m,0,sizeof (M)); for (int i=0; i<m; i++) {SCANF ("%d%d", &u,&v);//Change the mark Edge to record the number of edges.        otherwise will WA m[u][v]++;    m[v][u]++;    } int start=1;        If there are odd vertices, proceed from the odd vertex, otherwise from vertex 0 for (int i=1; i<=n; i++) {cnt=0;        for (int j=1; j<=n; J + +) {Cnt+=m[i][j];        } if (cnt&1) {start=i;    }} fleury (start); return 0;}


Hiho 50th Zhou Shi

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.