A62 - Depth First Search
https://atcoder.jp/contests/tessoku-book/tasks/math_and_algorithm_am
提出
TLE
code: python
n, m = map(int, input().split())
ab = list(map(int, input().split())) for _ in range(m)
g = {}
for i in range(1, n+1):
gi = []
for a, b in ab:
gb.append(a)
ga.append(b)
visited = False * (n+1)
visited0 = True
visited1 = True
now = g1
while len(now):
next = now.pop(0)
if visitednext:
continue
else:
visitednext = True
for v in gnext:
if visitedv:
continue
now.append(v)
print("The graph is connected.") if sum(visited) -1 == n else print("The graph is not connected.")
メモ
deque 使ったら AC
code: python
from collections import deque
n, m = map(int, input().split())
ab = list(map(int, input().split())) for _ in range(m)
g = {}
for i in range(1, n+1):
gi = []
for a, b in ab:
gb.append(a)
ga.append(b)
visited = False * (n+1)
visited0 = True
visited1 = True
now = deque(g1)
while len(now):
next = now.popleft()
if visitednext:
continue
else:
visitednext = True
for v in gnext:
if visitedv:
continue
now.append(v)
print("The graph is connected.") if sum(visited) -1 == n else print("The graph is not connected.")