백준
1. DFS
정해
import sys
input = sys.stdin.readline
n = int(input())
m = int(input())
graph = [[] for _ in range(n + 1)]
visit = []
for _ in range(m):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
def dfs(x):
for i in graph[x]:
if i not in visit:
visit.append(i)
dfs(i)
dfs(1)
print(len(visit) - 1)
틀렸습니다
import sys
input = sys.stdin.readline
n = int(input())
m = int(input())
graph = [[] for _ in range(n + 1)]
visit = []
for _ in range(m):
a, b = map(int, input().split())
graph[a].append(b)
def dfs(x):
for i in graph[x]:
if i not in visit:
visit.append(i)
dfs(i)
dfs(1)
print(len(visit))