from collections import deque
n, m = map(int, input().split())
arr = [[] for _ in range(n+1)]
for _ in range(m):
x, y = map(int, input().split())
arr[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 arr[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=" ")
# ํ ์คํธ ์ผ์ด์ค ๊ฐ์๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค.
# ๋ง์ฝ ์ฌ๊ท๋ฅผ ์ฌ์ฉํด์ ํ์ด์ผํ๋ ๋ฌธ์ ๋ผ๋ฉด setrecursionlimit์ ์ฐ๋ ๊ฒ์ ์ ํ์ด ์๋ ํ์๋ค.
# ํ์ด์ฌ์ ๊ธฐ๋ณธ ์ฌ๊ท ๊น์ด ์ ํ์ 1000์ผ๋ก ๋งค์ฐ ์๋ค.
# ๋ฐ๋ผ์ ์ฌ๊ท๋ก ๋ฌธ์ ๋ฅผ ํ๊ฒฝ์ฐ ์ด์ ํ์ ๊ฑธ๋ ค ์๋ฌ๋ฉ์ธ์ง๋ฅผ ๋ณผ์ ์๋ค.
# ๋ฐํ์ ์๋ฌ๊ฐ ๋๋ค.
import sys
sys.setrecursionlimit(100000)
def dfs(x,y):
visited[x][y] = True
directions = [ (-1, 0), (1, 0), (0, -1), (0, 1)]
# ์ํ ์ข์ฐ๋ก ์์ง์ผ ์ ์๋ ๋ฐฉํฅ์ ์ง์ ํด์ฃผ๊ณ ์ฌ๊ธฐ์ for๋ฌธ์ ๋๋ฆฐ๋ค.
for dx, dy in directions:
nx, ny = dx + x, dy+y
if nx < 0 or nx >=n or ny<0 or ny>=m:
continue
if array[nx][ny] and not visited[nx][ny]:
dfs(nx,ny)
for _ in range(int(input())):
m, n, k = map(int,input().split())
array=[[0]*m for _ in range(n)]
visited = [[False]*m for _ in range(n)]
for _ in range(k):
y, x = map(int,input().split())
array[x][y] = 1
result = 0
for i in range(n):
for j in range(m):
if array[i][j] and not visited[i][j]:
dfs(i,j)
result += 1
print(result)
from collections import deque
def bfs(x,y):
q = deque()
q.append((x,y))
visited[x][y] = True
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
while q:
x, y = q.popleft()
for dx, dy in directions:
nx, ny = x+dx, y+dy
if nx < 0 or nx >= n or ny < 0 or ny >= m:
continue
if array[nx][ny] and not visited[nx][ny]:
q.append((nx,ny))
visited[nx][ny]=True
for _ in range(int(input())):
m, n, k = map(int,input().split())
array=[[0]*m for _ in range(n)]
visited = [[False]*m for _ in range(n)]
for _ in range(k):
y, x = map(int,input().split())
array[x][y] = 1
result = 0
for i in range(n):
for j in range(m):
if array[i][j] and not visited[i][j]:
bfs(i,j)
result += 1
print(result)
com = int(input())
cnt = 0
adj = [[] for _ in range(com+1)]
visited = [False] * (com+1)
for i in range(int(input())):
x, y = map(int, input().split())
adj[x].append(y)
adj[y].append(x)
def dfs(v):
visited[v] = True
for e in adj[v]:
if not visited[e]:
dfs(e)
dfs(1)
for i in range(len(visited)):
if visited[i] == True:
cnt += 1
print(cnt-1)
2. BFS๋ก ํ์ด๋ณด๋ฉด,,,
from collections import deque
com = int(input())
cnt = 0
adj = [[] for _ in range(com+1)]
visited = [False] * (com+1)
for i in range(int(input())):
x, y = map(int, input().split())
adj[x].append(y)
adj[y].append(x)
def bfs(v):
q = deque([v])
visited[v] = True
while q:
v = q.popleft()
for e in adj[v]:
if not visited[e]:
q.append(e)
visited[e]= True
bfs(1)
for i in range(len(visited)):
if visited[i] == True:
cnt += 1
print(cnt-1)
import sys
n = int(sys.stdin.readline())
so = []
for i in range(n):
so.append(list(map(int, sys.stdin.readline().split())))
so.sort(key=lambda x: (x[0], x[1]))
for i in so:
print(i[0], i[1])