-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2617.py
More file actions
36 lines (28 loc) · 739 Bytes
/
2617.py
File metadata and controls
36 lines (28 loc) · 739 Bytes
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
32
33
34
35
36
# 구슬 찾기
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
N, M = map(int, input().split())
graphB = [[] for _ in range(N+1)]
graphS = [[] for _ in range(N+1)]
for _ in range(M):
a, b = map(int, input().split())
graphB[a].append(b)
graphS[b].append(a)
def dfs(graph, vertex):
visit[vertex] = True
for next_vertex in graph[vertex]:
if not visit[next_vertex]:
dfs(graph, next_vertex)
count = 0
for i in range(1, N + 1):
visit = [False] * (N + 1)
dfs(graphB, i)
if sum(visit) - 1 >= N // 2 + 1:
count += 1
else:
visit = [False] * (N + 1)
dfs(graphS, i)
if sum(visit) - 1 >= N // 2 + 1:
count += 1
print(count)