λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸƒ algorithm/boj

BOJ 6156 - Cow Contest

by HandHand 2021. 3. 8.

문제

λ°±μ€€ 온라인 저지 - 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

πŸ’¬ λŒ“κΈ€