[5022] Luo Valley Travel

Title Description

Y is a small OIer love to travel. She came to the country X, the cities are going to play again.

Small Y has learned that country the X-  n has n cities between  m m bidirectional roads. Each two-way road connecting the two cities. Two roads connecting the same to the city does not exist, nor is there a connection of a city and its own way. And, starting from any city, these roads can be reached by any of the other cities. Small Y can only go from one city to another by these roads.

Small Y travel program is as follows: any selected city as a starting point, and then start from the beginning every time you can choose a path connected to the current city, never been to a city, or along the first visit after the urban road back to the city. When the small Y back to the beginning, she may choose to end the trip or onward travel. It should be noted that the small Y requires travel scheme, each city will be visited.

In order to make their trip more meaningful, less Y decided upon each arrival of a new city (including the starting point), it's number recorded. She knows this will form a length of  n- n-sequences. She hoped that the lexicographically minimal sequence, can you help her? Two lengths are for  n- n-sequence  A A and  B B, if and only if there exists a positive integer  x when x, the following conditions are satisfied, we say that a sequence  A lexicographically A is less than  B B.

  • For any positive integer  . 1 ≤ I <X . 1 I < X, the sequence  A of the A  I I elements  A_i A I  and a sequence  B of the B  I I element  B_i B I  same.
  • Sequence  A of A,  x value less than the sequence of elements x  B B of  x the value of x elements.

Input Format

Total input file  m. 1 + m + . 1 line. The first line contains two integers  n-, m (m ≤ n-) n- , m ( m n- ), separated by an intermediate space.

Subsequently m rows, each row comprising two integers  U, v (. 1 ≤ U, v ≤ n-) U , v ( . 1 U , v n- ), that number is  U U and  v between cities have a v road, between two integers, separated by a space.

Output Format

The output file contains a line, n- n-integers, indicates lexicographically smallest sequence. Separated by a space between two adjacent integers.

Sample input and output

Input # 1
6 5 
1 3 
2 3 
2 5 
3 4 
4 6
Output # 1
1 3 2 5 4 6
Input # 2
6 6 
1 3 
2 3 
2 5 
3 4 
4 5 
4 6
Output # 2
1 3 2 4 5 6

Description / Tips

[Agreed] with the scale data

For  100 \% . 1 0 0 % of all the samples and data,  . 1 \ n-Le \ 5000 Le . 1 n- . 5 0 0 0, and m = n - 1 or  m = n- m = n-.

For different test points, the size of our data agreed as follows:

 

Solution to a problem; what that ring is not good at drawing ... 60 points is enough grace.

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
const int N=5001;
int n,m,xx,yy;
bool f[N][N];
int k,a[N];
bool vis[N];
void dfs1(int x,int now){
    if(now==n) return;
    bool flag=0;
    for(int i=1;i<=n;i++){
        if(vis[i]==0 && f[x][i]==1){
            view [i] = 1 ;
            flag=1;
            a[k++]=i;
            dfs1(i,now+1);
        }
    }
    if(flag==1) return;
}
void dfs2(int x,int now){
    if(now==n) return;
    bool flag=0;
    for(int i=1;i<=n;i++){
        if(vis[i]==0 && f[x][i]==1){
            view [i] = 1 ;
            flag=1;
            a[k++]=i;
            dfs2(i,now+1);
        }
    }
    if(flag==1) return;
}
int main () {
    scanf("%d %d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d %d",&xx,&yy);
        f[xx][yy]=f[yy][xx]=1;
    }
    if(m==n-1){
        vis[1]=1;
        a[k++]=1;
        dfs1(1,0);
        for(int i=0;i<n;i++)
            printf("%d ",a[i]);
    }
    if(m==n){
        vis[1]=1;
        a[k++]=1;
        dfs2(1,0);
        for(int i=0;i<n;i++)
            printf("%d ",a[i]);
    }
    //I am the best
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11775363.html