목차

도로 네트워크

ps
링크acmicpc.net/…
출처BOJ
문제 번호3176
문제명도로 네트워크
레벨플래티넘 4
분류

LCA

시간복잡도O((n+q)logn)
인풋사이즈n<=100,000, q<=100,000
사용한 언어Python 3.11
제출기록130676KB / 3044ms
최고기록2388ms
해결날짜2022/12/08

풀이

코드

"""Solution code for "BOJ 3176. 도로 네트워크".

- Problem link: https://www.acmicpc.net/problem/3176
- Solution link: http://www.teferi.net/ps/problems/boj/3176

Tags: [LCA]
"""

import sys
from teflib import tree as ttree

ROOT = 0


def main():
    N = int(sys.stdin.readline())
    wtree = [{} for _ in range(N)]
    for _ in range(N - 1):
        A, B, C = [int(x) for x in sys.stdin.readline().split()]
        wtree[A - 1][B - 1] = wtree[B - 1][A - 1] = (C, C)

    min_table = ttree.BinaryLifting(wtree, ROOT, func=min)
    max_table = ttree.BinaryLifting(wtree, ROOT, func=max)

    K = int(sys.stdin.readline())
    for _ in range(K):
        D, E = [int(x) for x in sys.stdin.readline().split()]
        print(
            min_table.path_query(D - 1, E - 1),
            max_table.path_query(D - 1, E - 1),
        )


if __name__ == '__main__':
    main()