Lunar New Year and a Wander (Codeforces 1106D)
Tags: Greedy, shortest paths

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 $$$a_1, a_2, \ldots, a_n$$$ 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 \ne 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 \leq n, m \leq 10^5$$$), 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 $$$u_i$$$ and $$$v_i$$$ ($$$1 \leq u_i, v_i \leq 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 $$$a_1, a_2, \ldots, a_n$$$ Bob can record.

Note: In the first sample, Bob's optimal wandering path could be $$$1 \rightarrow 2 \rightarrow 1 \rightarrow 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 \rightarrow 4 \rightarrow 3 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 1 \rightarrow 5$$$. Therefore, Bob will obtain the sequence $$$\{1, 4, 3, 2, 5\}$$$, which is the lexicographically smallest one.

Sample Inputs:
3 2
1 2
1 3

5 5
1 4
3 4
5 4
3 2
1 5

10 10
1 4
6 8
2 5
3 7
9 4
5 6
3 4
8 10
8 9
1 10

Sample Outputs:
1 2 3

1 4 3 2 5

1 4 3 7 9 8 6 5 2 10

Solution (Python):
import sys
from heapq import heappop, heappush
input = sys.stdin.readline

def solve():
    (n, m) = map(int, input().split())
    g = [[] for _ in range(n + 1)]
    for _ in range(m):
        (u, v) = map(int, input().split())
        g[u].append(v)
        g[v].append(u)
    ans = []
    heap = []
    vis = [0] * (n + 1)
    vis[1] = 1
    heappush(heap, 1)
    while heap:
        u = heappop(heap)
        ans.append(u)
        for v in g[u]:
            if not vis[v]:
                vis[v] = 1
                heappush(heap, v)
    print(*ans)
if __name__ == '__main__':
    solve()

Solution (Python):
import sys
import random
from types import *
from typing import *
from functools import cmp_to_key
from heapq import heappush, heappop
from collections import defaultdict, deque
input = sys.stdin.readline
(inf, mod) = (10 ** 18, 10 ** 9 + 7)
(N, M) = (2 * 10 ** 5, 10 ** 6)

def solve():
    (n, m) = map(int, input().split())
    a = [[] for _ in range(n + 1)]
    for i in range(m):
        (u, v) = map(int, input().split())
        a[u].append(v)
        a[v].append(u)
    was = [0] * (n + 1)
    was[1] = 1
    pq = []
    heappush(pq, 1)
    res = []
    while pq:
        fr = heappop(pq)
        res.append(fr)
        for to in a[fr]:
            if not was[to]:
                was[to] = 1
                heappush(pq, to)
    print(*res)

def main():
    tt = 1
    for _ in range(tt):
        solve()
main()

Solution (Python):
from heapq import heappush, heappop
(inf, mod) = (10 ** 18, 10 ** 9 + 7)
(N, M) = (2 * 10 ** 5, 10 ** 6)

def solve():
    (n, m) = map(int, input().split())
    a = [[] for _ in range(n + 1)]
    for i in range(m):
        (u, v) = map(int, input().split())
        a[u].append(v)
        a[v].append(u)
    visited = [0] * (n + 1)
    visited[1] = 1
    priority_queue = []
    heappush(priority_queue, 1)
    result = []
    while priority_queue:
        j = heappop(priority_queue)
        result.append(j)
        for i in a[j]:
            if not visited[i]:
                visited[i] = 1
                heappush(priority_queue, i)
    print(*result)
solve()