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 n nodes and m bidirectional edges. Initially Bob is at the node 11 and he records 11on 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,…,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 x is lexicographically smaller than a sequence y if and only if one of the following holds:
- x is a prefix of y, 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 y.
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条边的无向连通图,沿着边走,每次遇到一个没走过的新顶点就记录下来,让你输出字典序最小的记录方式。给的边会存在多条连接相同两个顶点的情况和自环的情况。
思路:建立邻接表把顶点i的所有邻接点压入vec[i]。字典序最小当然从1开始走,把当前最小可到达点取出来压入队列queue,然后再把当前最小可到达点的所有邻接点插入到set。set有自动排序去重的功能,默认升序,那么第一个元素就是我们要找的下一个最小可到达点
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<set>
#include<vector>
using namespace std;
int vis[100005]={0};
set<int>s;
vector<int>vec[100005];
int a[100005];
int main()
{
int n,m;
cin>>n>>m;
for(int t=0;t<m;t++)
{
int u,v;
scanf("%d%d",&u,&v);
if(u==v)
{
continue;
}
vec[u].push_back(v);
vec[v].push_back(u);
}
int pos=1,sum=1;
vis[1]=1;
a[1]=1;
while(sum<n)
{
for(int t=0;t<vec[pos].size();t++)
{
if(vis[vec[pos][t]]==0)
{
s.insert(vec[pos][t]);
}
}
set<int>::iterator it=s.begin();
pos=*it;
vis[pos]=1;
s.erase(it);
a[sum+1]=pos;
sum++;
}
for(int t=1;t<=sum;t++)
printf("%d ",a[t]);
return 0;
}