hihoCoder-1181-Oralu Shi (Fleury algorithm to find Euler path)

Source: Internet
Author: User
Tags cmath

#1181: Oralu Shi time limit:10000msSingle Point time limit:1000msMemory Limit:256MB
Describe

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


Fleury algorithm pseudo-code:

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


Here to notice how to delete the edge, you can use the vector's erase, you can also use a marker to determine whether the edge has been deleted, such as the edge to delete the value of 1.


Deletion of vectors:

#include <vector> #include <iostream>using namespace std; int main (int argc, char** argv) {    std::vector<int> vec;    for (int i=0;i<100;i++)    {         vec.push_back (i);    }     printf ("10:%d\n", vec[10]);    printf ("size:%d\n", Vec.size ());    printf ("**********************************\n");    Std::vector<int>::iterator it = Vec.begin () +10;    Vec.erase (it);     printf ("10:%d\n", vec[10]);    printf ("size:%d\n", Vec.size ());    return 0;}   Output//10:10//size:100//**********************************//10:11//size:99



AC Code (ERASE):

#include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long# Define INF 0x7fffffffusing namespace Std;int n, m;const int maxn = 1005;vector<int> g[maxn];int path[5005];int paths ize;void dfs (int u) {int D;while ((d = g[u].size ()) > 0) {int v = g[u][0];//cout << v << "" << D < < Endl;  G[u].erase (G[u].begin ()); int L = G[v].size (); for (int i = 0; I < L + +) {//Find the connection of V to u with u, because both sides are mutual, to remove if (g[v][i] = = u) {g[v].erase (G[v].begin () + i); break;}} DFS (v);} Path[pathsize + +] = u;} int main () {while (scanf ("%d%d", &n, &m)! = EOF) {for (int i = 0; i < m; i + +) {int u, v;scanf ("%d%d", &u, &AMP;V); G[u].push_back (v); G[v].push_back (u);} Pathsize = 0;int start = 1;while (! G[start].sIze ()) Start ++;//Find the first point with a connecting edge, but here the data is not so, later notice DFS (start);//Here Start write 1 can also AC for (int i = 0; i < pathsize-1; i + +) {Prin TF ("%d", Path[i]);} printf ("%d\n", Path[pathsize-1]);} return 0;}



AC Code (Mark):

#include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long# Define INF 0x7fffffffusing namespace Std;int n, m;const int maxn = 1005;vector<int> g[maxn];int path[5005];int paths  ize;void dfs (int u) {int d = g[u].size (); for (int i = 0; i < D; i + +) {int v = g[u][i];if (v! =-1) {G[u][i] = -1;int L = G[v].size (); for (int j = 0; J < L; j + +) {//Find the connecting edge of V connected to u, because both sides are mutual, to remove if (g[v][j] = = u) {g[v][j] = -1;break;}} DFS (v);}} Path[pathsize + +] = u;} int main () {while (scanf ("%d%d", &n, &m)! = EOF) {for (int i = 0; i < m; i + +) {int u, v;scanf ("%d%d", &u, &AMP;V); G[u].push_back (v); G[v].push_back (u);} int start = 1;while (! G[start].size ()) Start + +;p athsize = 0;dfs (start); for (int i = 0; i < pathsize-1; i + +) {printf ("%d", Path[i]);} printf ("%d\n", Path[pathsize-1]);} return 0;}








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hihoCoder-1181-Oralu Shi (Fleury algorithm to find Euler 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.