백준
1. 위상정렬, DFS
참고
import sys
from collections import defaultdict
N, M = tuple(map(int, sys.stdin.readline().split()))
adj_f = defaultdict(list)
adj_b = defaultdict(list)
for _ in range(M):
v1, v2 = tuple(map(int, sys.stdin.readline().split()))
adj_f[v1].append(v2)
adj_b[v2].append(v1)
def DFS_visit(adj, v1, parent, results):
if v1 in adj:
for v2 in adj[v1]:
if v2 not in parent:
parent[v2] = v1
DFS_visit(adj, v2, parent, results)
results.append(v1)
answers = set()
for v1 in range(1, N+1):
parent, results = {}, []
parent[v1] = None
DFS_visit(adj_f, v1, parent, results)
if len(results)-1 >= (N+1)//2:
answers.add(v1)
continue
parent, results = {}, []
parent[v1] = None
DFS_visit(adj_b, v1, parent, results)
if len(results)-1 >= (N+1)//2:
answers.add(v1)
print(len(answers))