Skip to content

Instantly share code, notes, and snippets.

@monhime
Created May 18, 2020 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monhime/db44d89dcf78813f42b1854fe22d20c5 to your computer and use it in GitHub Desktop.
Save monhime/db44d89dcf78813f42b1854fe22d20c5 to your computer and use it in GitHub Desktop.
ABC168 D問題 解答
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