λ¬Έμ
λ°±μ€ μ¨λΌμΈ μ μ§ - 2458λ²
νμ΄ κ³Όμ
νμλ€μ ν€ μ°¨μ΄ κ΄κ³κ° μ£Όμ΄μ§ λ μμ μ ν€ μμλ₯Ό νμ
ν μ μλ νμμ μλ₯Ό μ°Ύλ λ¬Έμ μ
λλ€.
ν€ μμλ₯Ό νμ
νκΈ° μν΄μλ μμ μ μ μΈν λͺ¨λ νμλ€κ³Όμ λμ κ΄κ³λ₯Ό νμ
ν μ μμ΄μΌ ν©λλ€.
λλ¬Έμ N
λͺ
μ νμλ€μ κ·Έλνμ μ μ , νμλ€ μ¬μ΄μ ν€ μ 보λ₯Ό κ°μ μ΄λΌκ³ νλ©΄
ν νμμ΄ λ€λ₯Έ λͺ¨λ νμμ λλ¬ κ°λ₯νκ±°λ λ€λ₯Έ νμμΌλ‘λΆν° ν΄λΉ νμμΌλ‘ λλ¬ κ°λ₯ν μ΄ μΈμμ΄ N - 1
μ΄μ΄μΌ ν©λλ€.νλ‘μ΄λ μμ¬
μκ³ λ¦¬μ¦μ νμ©ν΄μ λͺ¨λ νμλ€ μ¬μ΄μ λλ¬ κ°λ₯μ±μ νμ
νλ©΄ λ¬Έμ λ₯Ό ν΄κ²°ν μ μμ΅λλ€.
μ½λ
import sys
N, M = list(map(int, sys.stdin.readline().split()))
edges = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]
def make_graph():
dist = [[0] * N for _ in range(N)]
for frm, to in edges:
dist[frm - 1][to - 1] = 1
return dist
def floyd(dist):
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])
def solution():
dist = make_graph()
floyd(dist)
answer = 0
for frm in range(N):
acc = 0
for to in range(N):
if dist[frm][to] or dist[to][frm]:
acc += 1
if acc == N - 1:
answer += 1
return answer
print(solution())
λ°μν
'π algorithm > boj' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
BOJ 17829 - 222-νλ§ (0) | 2021.03.08 |
---|---|
BOJ 13905 - μΈλΆ (0) | 2021.03.08 |
BOJ 18111 - λ§μΈν¬λννΈ (0) | 2021.03.08 |
BOJ 1806 - λΆλΆν© (0) | 2021.03.08 |
BOJ 6186 - Best Grass (0) | 2021.03.08 |
π¬ λκΈ