백준 2606 파이썬

지민·2023년 1월 26일
0
post-thumbnail
import sys
import collections
input = sys.stdin.readline

queue = collections.deque([1])
n = int(input().rstrip())
graph = [[] for _ in range(n+1)]
visited = [False] * (n+1)
visited[1] = True

for _ in range(int(input().rstrip())):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)

while queue:
    now = queue.popleft()
    for i in graph[now]:
        if not visited[i]:
            queue.append(i)
            visited[i] = True

print(visited.count(True) - 1)

# ========== DFS 풀이도 있소 ================

input = sys.stdin.readline


def dfs(v):
    visited[v] = True
    for i in graph[v]:
        if not visited[i]:
            dfs(i)


queue = collections.deque([1])
n = int(input().rstrip())
graph = [[] for _ in range(n+1)]
visited = [False] * (n+1)
visited[1] = True

for _ in range(int(input().rstrip())):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)


while queue:
    now = queue.popleft()
    for i in graph[now]:
        if not visited[i]:
            queue.append(i)
            visited[i] = True

print(visited.count(True) - 1)
profile
남들 개발 공부할 때 일기 쓰는 사람

0개의 댓글