아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Sending Blessings

시간 제한2초메모리 제한512 MB

요약
정점 N개와 간선 N개로 이루어진 연결 그래프에서 Q개의 질의마다 두 도시 사이 경로의 최대 병목 용량을 구한다.
난이도

보통10점 중 7점

유형
그래프, 최소 신장 트리, 유니온 파인드
정답자
아직 제출이 없습니다

문제

There are N cities connected by N bridges in the country of Sayagueaing. The cities and the bridges are numbered from 1 to N. The ith bridge connects city Ai and city Bi and can be passed by a total of Ci people before the bridge collapses. It is guaranteed that any pair of cities are connected by a sequence of bridges. It is also guaranteed that any pair of cities are directly connected by at most one bridge.

At the start of every year, one of the N cities wants to send new year blessings to another one of the N cities. Specifically, we will consider the first Q years. At the start of the ith year, city Ui wants to send new year blessings to city Vi. To do so, the sender city sends as many residents as possible to the recipient city, one resident at a time. Each resident will go from city Ui to city Vi through one or more bridges. The president of Sayagueaing is wondering the number of residents that can reach the recipient city.

Note that at the end of every year, the conditions of the bridges are restored to their original state. In other words, all collapsed bridges will be rebuilt, and the ith bridge can be passed by a total of Ci people.

입력

Input begins with a line containing two integers: N Q (3 ≤ N ≤ 100 000; 1 ≤ Q ≤ 100 000) representing the number of cities in the country of Sayagueaing and the number of years to be considered, respectively. The next N lines, each contains three integers: Ai Bi Ci (1 ≤ Ai, Bi ≤ N; Ai ≠ Bi; 1 ≤ Ci ≤ 100 000) representing the bridges in the country of Sayagueaing. It is guaranteed that any pair of cities are connected by a sequence of bridges. It is also guaranteed that any pair of cities are directly connected by at most one bridge. The next Q lines, each contains two integers: Ui Vi (1 ≤ Ui, Vi ≤ N; Ui ≠ Vi) representing new year blessings to be sent.

출력

For each year in the same order as input, output in a line an integer representing the number of residents that can reach the recipient city at the start of the year.

예제1

  1. 예제 1

    입력
    4 3
    1 2 10
    2 3 2
    2 4 3
    3 4 1
    2 4
    1 4
    1 2
    
    예상 출력
    4
    4
    10