[Baekjoon] 1325 효율적인 해킹 pypy3

sorzzzzy·2021년 8월 10일
0

Baekjoon Algorithm

목록 보기
36/46
post-thumbnail

🏷 문제


💡 코드

from sys import stdin
from collections import deque

N, M = map(int, stdin.readline().split())

# 컴퓨터의 번호가 1~N 이므로
graph = [ [] for _ in range(N+1) ]

def find(x):   
    q = deque()
    q.append(x)
    visited = [0] * (N+1)
    visited[x] = 1
    cnt = 1
    while q:
        newx = q.popleft()
        for com in graph[newx]:
            if visited[com] == 0:
                visited[com] = 1
                q.append(com)
                cnt += 1
    return cnt

for _ in range(M):
    x, y = map(int, stdin.readline().split())
    # graph[x].append(y)
    graph[y].append(x)

res_list = []
max_tmp = -1e9
# 1번 컴퓨터부터 탐색 시작
for idx in range(1, N+1):
    res = find(idx)
    if res > max_tmp:
        # 컴퓨터 개수가 더 많다면, 그 컴퓨터 번호로 업데이트
        res_list = [idx]
        max_tmp = res
    elif res == max_tmp:
        # 컴퓨터 개수가 같다면, 뒤에 추가
        res_list.append(idx)
    else:
        continue

print(*res_list)

🔑

profile
Backend Developer

0개의 댓글