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

BOJ 15723 - n단 논법

by HandHand 2021. 3. 18.

문제

λ°±μ€€ 온라인 저지 - 15723번

풀이 κ³Όμ •

n단 논법 을 λ§Œμ‘±ν•˜λŠ”μ§€ νŒλ‹¨ν•˜λŠ” λ¬Έμ œμž…λ‹ˆλ‹€.

μš°μ„  주어진 λ…Όμ œλ₯Ό λ°”νƒ•μœΌλ‘œ λ°©ν–₯ κ·Έλž˜ν”„λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
이후 νŠΉμ • 논법이 λͺ¨μˆœμ΄ μ—†λŠ”μ§€ νŒλ‹¨ν•˜κΈ° μœ„ν•΄μ„œλŠ” a is d 일 λ•Œ a μ—μ„œ d 에 도달 κ°€λŠ₯ν•œμ§€ νŒλ‹¨ν•˜λ©΄ λ©λ‹ˆλ‹€.
λ”°λΌμ„œ μƒμ„±ν•œ κ·Έλž˜ν”„μ— ν”Œλ‘œμ΄λ“œ 와샬 μ•Œκ³ λ¦¬μ¦˜μ„ μ΄μš©ν•΄μ„œ λͺ¨λ“  정점에 λŒ€ν•΄ 도달 κ°€λŠ₯성을 νŒλ‹¨ν•˜λ©΄ λ©λ‹ˆλ‹€.

μ½”λ“œ


import sys


N = int(input())
graph = [[0] * 26 for _ in range(26)]
for _ in range(N):
    frm, to = list(sys.stdin.readline().strip().split(' is '))
    graph[ord(frm) - ord('a')][ord(to) - ord('a')] = 1

M = int(input())
questions = []
for _ in range(M):
    frm, to = list(sys.stdin.readline().strip().split(' is '))
    questions.append([ord(frm) - ord('a'), ord(to) - ord('a')])


def floyd():
    global graph

    for k in range(26):
        for u in range(26):
            for v in range(26):
                graph[u][v] = graph[u][v] or (graph[u][k] and graph[k][v])


def solution():
    floyd()

    for frm, to in questions:
        if graph[frm][to]:
            print('T')
        else:
            print('F')


solution()
λ°˜μ‘ν˜•

'πŸƒ algorithm > boj' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

BOJ 18238 - ZOAC2  (0) 2021.03.18
BOJ 14754 - Pizza Boxes  (0) 2021.03.18
BOJ 14496 - κ·ΈλŒ€, κ·Έλ¨Έκ°€ λ˜μ–΄  (0) 2021.03.16
BOJ 9311 - Robot in a Maze  (0) 2021.03.14
BOJ 20005 - 보슀λͺ¬μŠ€ν„° μ „λ¦¬ν’ˆ  (0) 2021.03.14

πŸ’¬ λŒ“κΈ€