문제 확인하기
1. Python (memory: 32404KB, time: 220ms)
import sys
def depth_first_search(current_node, count):
visited[current_node] = 1
for neighbor in graph[current_node]:
if visited[neighbor] == 0:
count = depth_first_search(neighbor, count+1)
return count
for _ in range(int(sys.stdin.readline())):
num_nodes, num_edges = map(int, sys.stdin.readline().split())
graph = [[] for _ in range(num_nodes+1)]
for _ in range(num_edges):
u, v = map(int, sys.stdin.readline().split())
graph[u].append(v)
graph[v].append(u)
visited = [0]*(num_nodes+1)
visited[1] = 1
count = depth_first_search(1, 0)
print(count)