티스토리 뷰

# 2606 바이러스
import sys

nodes = int(sys.stdin.readline())
pairs = int(sys.stdin.readline())
node_set = set()
adjacencyList = [[] for _ in range(nodes+1)]
visitedList = []
for _ in range(pairs):
    a, b = map(int, sys.stdin.readline().split())
    if a not in node_set:
        node_set.add(a)
    if b not in node_set:
        node_set.add(b)
    if a not in adjacencyList[b]:
        adjacencyList[b].append(a)
    if b not in adjacencyList[a]:
        adjacencyList[a].append(b)

for i in adjacencyList:
    i.sort()
# print(adjacencyList)
stack = [1]

while stack:
    current = stack.pop()
    for neighbor in adjacencyList[current]:
        if not neighbor in visitedList:
            stack.append(neighbor)
    if current not in visitedList:
        visitedList.append(current)

# print(visitedList)
print(len(visitedList)-1) # 1은 제외
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함