欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1106D.Lunar New Year and a Wander
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 nodes and bidirectional edges. Initially Bob is at the node and he records 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 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 is lexicographically smaller than a sequence if and only if one of the following holds:
- is a prefix of , but (this is impossible in this problem as all considered sequences have the same length);
- in the first position where and differ, the sequence has a smaller element than the corresponding element in .
Input
The first line contains two positive integers and (), denoting the number of nodes and edges, respectively.
The following lines describe the bidirectional edges in the graph. The -th of these lines contains two integers and (), representing the nodes the -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 Bob can record.
Note
In the first sample, Bob's optimal wandering path could be . Therefore, Bob will obtain the sequence , which is the lexicographically smallest one.
In the second sample, Bob's optimal wandering path could be $1 \rightarrow 4 \rightarrow 3 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 1 \rightarrow 5$. Therefore, Bob will obtain the sequence , which is the lexicographically smallest one.
Samples
3 2
1 2
1 3
1 2 3
5 5
1 4
3 4
5 4
3 2
1 5
1 4 3 2 5
10 10
1 4
6 8
2 5
3 7
9 4
5 6
3 4
8 10
8 9
1 10
1 4 3 7 9 8 6 5 2 10
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |