C - Lunar New Year and a Wander 优先队列 CodeForces - 1106D

传送门
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 1 and he records 1 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,…,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≠y (this is impossible in this problem as all considered sequences have the same length);
in the first position where x and y differ, the sequence x has a smaller element than the corresponding element in y.
Input
The first line contains two positive integers n and m (1≤n,m≤105), denoting the number of nodes and edges, respectively.

The following m lines describe the bidirectional edges in the graph. The i-th of these lines contains two integers ui and vi (1≤ui,vi≤n), representing the nodes the i-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,…,an Bob can record.

Examples
Input
3 2
1 2
1 3
Output
1 2 3
Input
5 5
1 4
3 4
5 4
3 2
1 5
Output
1 4 3 2 5
Input
10 10
1 4
6 8
2 5
3 7
9 4
5 6
3 4
8 10
8 9
1 10
Output
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→3. Therefore, Bob will obtain the sequence {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→5. Therefore, Bob will obtain the sequence {1,4,3,2,5}, which is the lexicographically smallest one.
题意:给你一个n个点,m条边的无向连通图。让你找出一条可以遍历所有定点的,字典序最小的路径。
思路:vector定义数组 存储临接点。
每次将临界点压入队列,使用优先队列输出最小的邻接点。
ps; 定义优先队列 从小到大输出。
priority_queue<int,vector,greater >q;//小到大

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<cctype>
#include<stack>
#include<map>
#include<string>
#include<cstdlib>
#define ll long long
#define N 100010
using namespace std;
priority_queue<int,vector<int>,less<int> >p;//大到小
priority_queue<int,vector<int>,greater<int> >q;//小到大
const ll maxn = 100000 + 5;
vector<int>a[maxn];//定义一个动态数组   二维数组也可以吧。
bool vis[maxn];//记录未被遍历的
int main()
{
    ll n,m,u,v;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        cin>>u>>v;
        a[u].push_back(v);
        a[v].push_back(u);
    }
    q.push(1);//先进先出,出小的。
    while(!q.empty())
    {
        int u;
        u=q.top();//每次都是上一个所临接的最小点出来;//在优先队列中,q.top(),在队列中,front();
        q.pop();
        if(vis[u])
            continue;
        vis[u]=1;
        cout<<u<<" ";
        for(int i=0;i<a[u].size();i++)
        {
            if(!vis[a[u][i]])//找u的临接点;
            q.push(a[u][i]);
        }

    }
        return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值