Solve ABC168D in Python

Introduction

I couldn't solve it at ABC yesterday, and I was frustrated and couldn't sleep at night, so I'll review it.

.. (Double Dots)

** Thoughts ** Due to restrictions, the graphs given are concatenated because it is possible to move between rooms using multiple roads. If it is a connected graph, it will always be like construction. If you submit it with print ('No'), it will be 0AC. The solution is to search for the signpost from 1 to the same depth, and when you reach a room deeper than you, place the signpost to your room. This is easy to implement in BFS.

ver is a list of rooms that can be reached from ver [i] in one go

from collections import deque
n, m = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(m)]

q = deque([])
path = [-1] * n
ver = [[] for _ in range(n)]
for i in range(m):
    ab[i].sort()
    if ab[i][0] == 1:
        q.append([ab[i][0]-1,ab[i][1]-1])
        #path[ab[i][1]-1] = 1
    ver[ab[i][0]-1].append(ab[i][1]-1)
    ver[ab[i][1]-1].append(ab[i][0]-1)

def bfs(q, visited_list):
    while len(q) > 0:
        now = q.popleft()
        go = now[1]
        if visited_list[go] == -1:
            visited_list[go] = now[0]+1
            for i in range(len(ver[go])):
                q.append([go,ver[go][i]])
    return visited_list
ans = bfs(q,path)
print('Yes')
for i in range(1,n):
    print(ans[i])

Computational complexity is $ O (N + M) $

Summary

It was a very basic problem. Yesterday, I felt like I was spinning, so I will do my best to complete one at the next AGC. see you.

Digression

Graph problems are easy to think of using this.

Recommended Posts

Solve ABC168D in Python
Solve ABC167-D in Python
Solve ABC159-D in Python
Solve ABC146-C in Python
Solve ABC098-C in Python
Solve ABC169 in Python
Solve ABC160-E in Python
Solve ABC176 E in Python
Solve Wooldridge exercises in Python
Solve ABC175 D in Python
[Python] ABC175D
Solve optimization problems in Python
Solve Atcoder ABC169 A-D in Python
Solve ABC036 A ~ C in Python
Solve ABC037 A ~ C in Python
Solve ordinary differential equations in Python
Quadtree in Python --2
Python in optimization
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
[Python] DP ABC184D
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
[Python] UnionFind ABC177D
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
ABC 157 D --Solve Friend Suggestions in Python!