Codeforce-1106-D. Lunar New Year and a Wander(DFS遍历+vector存图+set)
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;
}
Codeforce-1106-D. Lunar New Year and a Wander(DFS遍历+vector存图+set)的更多相关文章
- D. Lunar New Year and a Wander bfs+优先队列
D. Lunar New Year and a Wander bfs+优先队列 题意 给出一个图,从1点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最 ...
- Codeforces 1106 E. Lunar New Year and Red Envelopes 优先队列+dp
题意大致是Bob新年拿红包,每个红包可以在s-t时间内取,但是取了之后得在d+1时间开始才能继续取红包. 同时他女儿能在m个时间点阻止他取红包,求女儿阻止后Bob取得的w总和最小值. Bob取红包的策 ...
- CF - 1106 E Lunar New Year and Red Envelopes DP
题目传送门 题解: 首先要处理出每个时间点会选择哪一个线段. 对于这个问题,可以用multiset去维护信息. 当时间线开始的时候,往mutiset里面插入这个信息,当时间线结束的时候,删除这个信息. ...
- Codeforces Round #536 (Div. 2)--1106D - Lunar New Year and a Wander
https://codeforces.com/contest/1106/problem/D 题意:求出字典序最小的走法 解法:走到每个点,都选取与这个点连通的序号最小的点,并且这个序号最小的点没有被访 ...
- 【Codeforces 1106D】Lunar New Year and a Wander
[链接] 我是链接,点我呀:) [题意] 让你遍历n个节点,访问过的节点不操作. 如果是没有访问过的点,那就把它加到序列的末尾. 问你形成的最小字典序的序列是多少. [题解] 显然每次找最小的标号 用 ...
- Codeforces Round #538 D. Lunar New Year and a Wander
题面: 传送门 题目描述: Bob想在公园散步.公园由n个点和m条无向边组成.当Bob到一个未经过的点时,他就会把这个点的编号记录在笔记本上.当且仅当Bob走完所有的点,他才会停下来.这时,Bob的笔 ...
- PAT甲级——1106 Lowest Price in Supply Chain(BFS)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90444872 1106 Lowest Price in Supp ...
- 【Star CCM+实例】开发一个简单的计算流程.md
流程开发在CAE过程中处于非常重要的地位. 主要的作用可能包括: 将一些经过验证的模型隐藏在流程中,提高仿真的可靠性 将流程封装成更友好的界面,降低软件的学习周期 流程开发实际上需要做非常多的工作,尤 ...
- 题解-Codeforces1106全套
因为参加完wc后心情很差,而且在广州过年没Ubuntu,所以就没打这场比赛了,结果这套题全部1A了,现在看来真是错失良机 结果这场不计rating 今天是除夕,大家节日快乐 A. Lunar New ...
随机推荐
- 张超超OC基础回顾02_成员变量(属性),局部变量,全局变量的区别
成员变量: 写在类声明的大括号中的变量, 我们称之为 成员变量(属性, 实例变量) 成员变量只能通过对象来访问 注意: 成员变量不能离开类, 离开类之后就不是成员变量 成员变量不能在定义的同时进行初始 ...
- Docker02 基本命令、开发环境搭建、docker安装nginx、Dockerfile、路径挂载
1 基本命令 1.1 docker相关 centos6.5 安装docker环境 >sudo yum install -y http://mirrors.yun-idc.com/epel/6/i ...
- sqLSERVER 计划缓存
在这一期的性能调优培训里,我想详细谈下SQL Server里计划缓存及其副作用.在上一周你已经学到,每个提交给SQL Server的逻辑查询会编译成物理执行计划.那个执行计划然后会被缓存,即被称为计划 ...
- SqlServer-geography && Spatial result
说起geography(地理)这个类型,我感觉好陌生,以前真的没有见过,今天在查询某个Address表的时候,却发现了新大陆——Spatial result(空间的结果). (1)表的结构 (2)查询 ...
- Mac下MongoDB enterprise版的安装
1. 访问mongodb下载中心,https://www.mongodb.com/download-center#enterprise,选择OS X x64系统,点击下载,可能会出一个页面让你填写联系 ...
- sublime text 3安装 vue插件
1.上一个章节讲到Vue.js的环境安装,这一章节主要是针对ST3 如何安装vue插件,来快速的进行vue组件代码的编写. (内容转载自:https://www.cnblogs.com/bluedoc ...
- 在Asp.Net中使用amChart统计图
怎么在自己的ASP.NET页面插入可动态更新的数据统计图呢?网上的资源倒是不少(Fusioncharts.amCharts……),在这些资源中有一个比较好用:amChart,这个工具很炫,还能与用户交 ...
- 如何获取.properties配置文件
如何获取.properties配置文件 分析思路: 先使用流和文件关联,即读取文件 再读取文件内容,一行一行读取 字符分割“=” 键值对 然后把键值对放到集合中去 但是Properties类里面有方 ...
- 实践作业3:白盒测试----简单介绍被测系统DAY4
本次被测软件是高校学生信息管理系统,和上次黑盒测试选用一样的系统,这样做的好处在于我们对系统比较熟悉,而且可以更好的比较黑盒测试与白盒测试的区别,采用MySQL Workbench 6.3,在MyEc ...
- What’s the Difference Between a Value Provider and Model Binder?
ASP.NET MVC 3 introduced the ability to bind an incoming JSON request to an action method parameter, ...