효율적인 해킹 [1325] with 파이썬 본문
https://www.acmicpc.net/problem/1325
1325번: 효율적인 해킹
첫째 줄에, N과 M이 들어온다. N은 10,000보다 작거나 같은 자연수, M은 100,000보다 작거나 같은 자연수이다. 둘째 줄부터 M개의 줄에 신뢰하는 관계가 A B와 같은 형식으로 들어오며, "A가 B를 신뢰한�
www.acmicpc.net
from collections import deque
n, m = map(int, input().split())
adj = [[] for _ in range(n+1)]
for _ in range(m):
x, y = map(int, input().split())
adj[y].append(x)
def bfs(v):
q = deque([v])
visited = [False] * (n+1)
visited[v] = True
count = 1
while q:
v = q.popleft()
for e in adj[v]:
if not visited[e]:
q.append(e)
visited[e] = True
count += 1
return count
result = []
max_value = -1
for i in range(1, n+1):
c = bfs(i)
if c > max_value:
result = [i]
max_value = c
elif c == max_value:
result.append(i)
max_value = c
for e in result:
print(e, end=' ')
'Algorithms > BFS and DFS' 카테고리의 다른 글
바이러스 [2606] with 파이썬 (0) | 2020.06.02 |
---|---|
벽 부수고 이동하기 [2206] with 파이썬 (0) | 2020.06.02 |
탈출 [3055] with 파이썬 (0) | 2020.06.02 |
DFS와 BFS 분류 [1260] with 파이썬 (0) | 2020.06.01 |
유기농 배추 [1012] with 파이썬 (0) | 2020.06.01 |