Codeforces Round #536 (Div. 2) D. Lunar New Year and a Wander

Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.

The park can be represented as a connected graph with nn nodes and mm bidirectional edges. Initially Bob is at the node 11 and he records 11 on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes a1,a2,…,ana1,a2,…,an is recorded.

Wandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it.

A sequence xx is lexicographically smaller than a sequence yy if and only if one of the following holds:

  • xx is a prefix of yy, but x≠yx≠y (this is impossible in this problem as all considered sequences have the same length);
  • in the first position where xx and yy differ, the sequence xx has a smaller element than the corresponding element in yy.

Input

The first line contains two positive integers nn and mm (1≤n,m≤1051≤n,m≤105), denoting the number of nodes and edges, respectively.

The following mm lines describe the bidirectional edges in the graph. The ii-th of these lines contains two integers uiui and vivi (1≤ui,vi≤n1≤ui,vi≤n), representing the nodes the ii-th edge connects.

Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected.

Output

Output a line containing the lexicographically smallest sequence a1,a2,…,ana1,a2,…,an Bob can record.

Examples

input

Copy

3 2
1 2
1 3

output

Copy

1 2 3 

input

Copy

5 5
1 4
3 4
5 4
3 2
1 5

output

Copy

1 4 3 2 5 

input

Copy

10 10
1 4
6 8
2 5
3 7
9 4
5 6
3 4
8 10
8 9
1 10

output

Copy

1 4 3 7 9 8 6 5 2 10 

Note

In the first sample, Bob's optimal wandering path could be 1→2→1→31→2→1→3. Therefore, Bob will obtain the sequence {1,2,3}{1,2,3}, which is the lexicographically smallest one.

In the second sample, Bob's optimal wandering path could be 1→4→3→2→3→4→1→51→4→3→2→3→4→1→5. Therefore, Bob will obtain the sequence {1,4,3,2,5}{1,4,3,2,5}, which is the lexicographically smallest one.

题意:就是给一张n个点的m条边的无向图让你输出最小字典序的n个点的遍历循序。

思路:队列+set来解决即可。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<set>
#define LL long long

using namespace std;
const int maxn=1e5+100;
int vis[maxn];
vector<int>mp[maxn];
set<int>st;
vector<int>gg;
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        mp[a].push_back(b);
        mp[b].push_back(a);
    }
    int pos=1;
    vis[pos]=1;
    gg.push_back(pos);
    int cnt=0;
    while(cnt<n-1)
    {
        for(int i=0;i<mp[pos].size();i++)
        {
            if(vis[mp[pos][i]])
            {
                continue;
            }
            st.insert(mp[pos][i]);
        }
        set<int>::iterator it=st.begin();
        vis[*it]=1;
        pos=*it;
       // cout<<pos<<endl;
        gg.push_back(pos);
        st.erase(it);
        cnt++;
        //cout<<cnt<<endl;
    }
    for(int i=0;i<gg.size();i++)
    {
        if(i!=gg.size()-1)
            printf("%d ",gg[i]);
        else
            printf("%d\n",gg[i]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wxl7777/article/details/86927490
今日推荐