BOJ - 1613 - 역사

Updated:

import sys

def solution():
    N, K = map(int, sys.stdin.readline().split())
    arr = [[0] * N for _ in range(N)]
    for _ in range(K):
        x, y = map(int, sys.stdin.readline().split())
        arr[x-1][y-1] = 1

    for k in range(N):
        for i in range(N):
            for j in range(N):
                if arr[i][k] and arr[k][j]:
                    arr[i][j] = 1

    S = int(input())
    for _ in range(S):
        x, y = map(int, sys.stdin.readline().split())
        if arr[x-1][y-1] == 1:
            print(-1)
        elif arr[y-1][x-1] == 1:
            print(1)
        elif arr[x-1][y-1] == 0:
            print(0)

solution()

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

Categories:

Updated:

Leave a comment