Lunar New Year and a Wander

Posted czy-power

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lunar New Year and a Wander相关的知识,希望对你有一定的参考价值。

D. Lunar New Year and a Wander
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 xy (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 (1n,m105), 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 (1ui,vin), 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
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 1213. 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 14323415. Therefore, Bob will obtain the sequence {1,4,3,2,5}, which is the lexicographically smallest one.

 

 

#include<bits/stdc++.h>
#define REP(i, a, b) for(int i = (a); i <= (b); ++ i)
#define REP(j, a, b) for(int j = (a); j <= (b); ++ j)
#define PER(i, a, b) for(int i = (a); i >= (b); -- i)
using namespace std;
const int maxn=2e5+5;
template <class T>
inline void rd(T &ret){
    char c;
    ret = 0;
    while ((c = getchar()) < 0 || c > 9);
    while (c >= 0 && c <= 9){
        ret = ret * 10 + (c - 0), c = getchar();
    }
}
priority_queue<int,vector<int>,greater<int> >q;
vector<int>ans;
bool vis[maxn];
int head[maxn],n,m,tot;
struct node{
    int to,nx;
}p[maxn];
void addedge(int s,int t){
     p[++tot].to=t,p[tot].nx=head[s],head[s]=tot;
}
int main()
{
    rd(n),rd(m);
    REP(i,1,m){
        int u,v;
        rd(u),rd(v);
        addedge(u,v),addedge(v,u);
    }
    q.push(1);
    while(!q.empty()){
         int cur=q.top();
         q.pop();
         if(vis[cur])continue;
         vis[cur]=true;
         ans.push_back(cur);
         for(int i=head[cur];i;i=p[i].nx){
              int to=p[i].to;
              if(vis[to])continue;
             // vis[to]=1;
              q.push(to);
         }
    }
    for(int i=0;i<ans.size();i++)cout<<ans[i]<< ;
    return 0;
}

 

以上是关于Lunar New Year and a Wander的主要内容,如果未能解决你的问题,请参考以下文章

CF1106F Lunar New Year and a Recursive Sequence

Codeforces 1106C Lunar New Year and Number DivisionLunar |数学

F. Lunar New Year and a Recursive Sequence(矩阵快速幂+BSGS)

CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs

CF1106F Lunar New Year and a Recursive Sequence(矩阵快速幂+bsgs+exgcd)

Codeforces 536F Lunar New Year and a Recursive Sequence | BSGS/exgcd/矩阵乘法