Created
May 18, 2020 00:21
-
-
Save monhime/db44d89dcf78813f42b1854fe22d20c5 to your computer and use it in GitHub Desktop.
ABC168 D問題 解答
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def input(): return sys.stdin.readline().rstrip() | |
from collections import deque | |
def main(): | |
n,m=map(int,input().split()) | |
graph=[[] for _ in range(n)] | |
for i in range(m): | |
a,b=map(int,input().split()) | |
graph[a-1].append(b-1) | |
graph[b-1].append(a-1) | |
queue=deque([0]) | |
visited=[False]*n | |
visited[0]=True | |
ans=[0]*n | |
while queue: | |
node=queue.popleft() | |
for xnode in graph[node]: | |
if visited[xnode]:continue | |
ans[xnode]=node+1 | |
visited[xnode]=True | |
queue.append(xnode) | |
print("Yes") | |
for a in ans[1:]: | |
print(a) | |
if __name__=='__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment