BOJ - 14641 - Secret Chamber at Mount Rushmore

Updated:

import sys
_INF = 1e9
def solution():
    M, N = map(int, sys.stdin.readline().split())
    matrix = [[_INF for _ in range(26)] for _ in range(26)]
    for _ in range(M):
        start, end = map(str, sys.stdin.readline().split())
        matrix[ord(start)-97][ord(end)-97] = 1

    for k in range(26):
        for i in range(26):
            for j in range(26):
                matrix[i][j] = min(matrix[i][j], matrix[i][k] + matrix[k][j])

    for _ in range(N):
        flag = False
        source, target = map(str, sys.stdin.readline().split())
        if len(source) != len(target):
            print('no')
            continue

        for i in range(len(source)):
            if source[i] == target[i]: continue
            if matrix[ord(source[i])-97][ord(target[i])-97] == _INF:
                flag = True
                break
        if flag: print('no')
        else: print('yes')

solution()

https://www.acmicpc.net/problem/14641

Categories:

Updated:

Leave a comment