Skip to content

Instantly share code, notes, and snippets.

@inspirit941
Created February 24, 2020 05:31
Show Gist options
  • Save inspirit941/e3481346943be564b357763a3a283490 to your computer and use it in GitHub Desktop.
Save inspirit941/e3481346943be564b357763a3a283490 to your computer and use it in GitHub Desktop.
import sys
from collections import deque
def bfs(start, connected):
queue = deque()
queue.append(start)
visited = [0 for _ in range(N+1)]
count = 1
while queue:
current = queue.popleft()
visited[current] = 1
if connected[current]:
for _next in connected[current]:
if not visited[_next]:
count += 1
visited[_next] = 1
queue.append(_next)
return count
N, M = map(int, sys.stdin.readline().split())
connected = [[] for _ in range(N+1)]
for _ in range(M):
A, B = map(int, sys.stdin.readline().split())
connected[B].append(A)
table = dict()
for i in range(1, N+1):
if connected[i]:
table[i] = bfs(i, connected)
max_value = max(table.values())
print(*sorted([i for (i,j) in table.items() if j == max_value]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment