λ¬Έμ
λ°±μ€ μ¨λΌμΈ μ μ§ - 6156λ²
νμ΄ κ³Όμ
λ μλ€ κ°μ μΈμ κ²½κΈ° κ²°κ³Όκ° μ£Όμ΄μ§ λ μμλ₯Ό κ²°μ ν μ μλ μλ€μ μλ₯Ό ꡬνλ λ¬Έμ μ
λλ€.
νΉμ μμ μμλ₯Ό κ²°μ νκΈ° μν΄μλ ν΄λΉ μμκ² μ§ μλ€μ μμ ν΄λΉ μλ₯Ό μ΄κΈ΄ μλ€μ ν©μ΄ N - 1
μ΄μ΄μΌ ν©λλ€.
λλ¬Έμ κ° μλ€μ κ·Έλνμ μ μ μ΄λΌκ³ μκ°νλ€λ©΄ λͺ¨λ μλ€ μ¬μ΄μ λλ¬ κ°λ₯μ±μ νλ‘μ΄λ μμ¬
μκ³ λ¦¬μ¦μ μ΄μ©ν΄ νμ
νκ³
κ° μλ€μ λν΄μ νΉμ μ A
μ λλ¬ κ°λ₯ν μ μ μ μμ A
μμ λλ¬ κ°λ₯ν μλ€μ μλ₯Ό μΈμ΄μ£Όλ©΄ λ©λλ€.
μ½λ
import sys
N, M = list(map(int, sys.stdin.readline().split()))
results = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]
def make_dist():
dist = [[0] * N for _ in range(N)]
for winner, loser in results:
dist[loser - 1][winner - 1] = 1
return dist
def floyd(dist):
for v in range(N):
dist[v][v] = 1
for k in range(N):
for u in range(N):
for v in range(N):
dist[u][v] = dist[u][v] or (dist[u][k] and dist[k][v])
determined = 0
for v in range(N):
reachable = 0
for to in range(N):
if dist[v][to] and to != v:
reachable += 1
for frm in range(N):
if dist[frm][v] and frm != v:
reachable += 1
if reachable == N - 1:
determined += 1
return determined
def solution():
dist = make_dist()
answer = floyd(dist)
return answer
print(solution())
λ°μν
'π algorithm > boj' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
BOJ 13335 - νΈλ (0) | 2021.03.08 |
---|---|
BOJ 6497 - μ λ ₯λ (0) | 2021.03.08 |
BOJ 9625 - BABBA (0) | 2021.03.08 |
BOJ 11952 - μ’λΉ (0) | 2021.03.08 |
BOJ 20055 - μ»¨λ² μ΄μ΄ λ²¨νΈ μμ λ‘λ΄ (0) | 2021.03.08 |
π¬ λκΈ