hihocoder#1181 (Euler path)

Source: Internet
Author: User

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
Note: This topic must be traversed from node 1th to AC. When the non-direction graph is deleted, the forward edge associated with the u,v is deleted.
The Euler path of the non-direction graph is conveniently used in code 1, and it is convenient to use the forward direction to calculate Euler path.
#include <cstdio>#include<cstring>#include<vector>using namespacestd;Const intmaxn=1005;structedge{intu,v; intGetto (intu) {if(u== This-&GT;U)returnv; Else return  This-u; }}es[5005];intn,m;intdeg[maxn],vis[5005];intpath[5005],top;vector<int>ARC[MAXN];voidDfsintu) {     for(intI=0, Size=arc[u].size (); i<size;i++)    {        intId=Arc[u][i]; if(!Vis[id]) {Vis[id]=1; intv=es[id].getto (U);        DFS (v); }} path[top++]=u;}intMain () { while(SCANF ("%d%d", &n,&m)! =EOF) {memset (deg,0,sizeof(deg)); memset (Vis,0,sizeof(VIS));  for(intI=1; i<=n;i++) {arc[i].clear (); }         for(intI=1; i<=m;i++)        {            intu,v; scanf ("%d%d",&u,&v); ES[I].U=u; ES[I].V=v;            Arc[u].push_back (i);            Arc[v].push_back (i); Deg[u]++; DEG[V]++; }        /*WA??                for (int i=1;i<=n;i++) {if (deg[i]%2==1) {DFS (i);            Break }        }        */DFS (1);  for(intI=0; i<top-1; i++) {printf ("%d", Path[i]); } printf ("%d\n", path[top-1]); }    return 0;}

Forward-to-star implementations.

#include <cstdio>#include<cstring>using namespacestd;Const intmaxn=1005;structedge{intto,net; BOOLtag;} ES[MAXN*Ten];intn,m;intHead[maxn],tot;intpath[5005],top;intDEG[MAXN];voidAddedge (intUintv) {es[tot].to=v; Es[tot].tag=false; Es[tot].net=Head[u]; Head[u]=tot++;}voidDfsintu) {     for(inti=head[u];i!=-1; i=es[i].net) {        if(!Es[i].tag) {            intv=es[i].to;  for(intj=head[v];j!=-1; j=es[j].net) {                if(es[j].to==u&&!Es[j].tag) {Es[j].tag=true;  Break; }} Es[i].tag=true;        DFS (v); }} path[top++]=u;}intMain () {memset (head,-1,sizeof(head)); scanf ("%d%d",&n,&m);  for(intI=0; i<m;i++)    {        intu,v; scanf ("%d%d",&u,&v);        Addedge (U,V);        Addedge (V,u); Deg[u]++; DEG[V]++; }    /*wrong for (int i=1;i<=n;i++) {if (deg[i]%2==1) {DFS (i);        Break }    }    */DFS (1);//Accepted     for(intI=0; i<top-1; i++) {printf ("%d", Path[i]); } printf ("%d\n", path[top-1]); return 0;}

hihocoder#1181 (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.