21019_DistanceQuery

2022-5-16 18:18| 发布者: Hocassian| 查看: 42| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-3-89504759735000-Problem List-采集的数据-后羿采集器.html

Pro.ID

21019

Title

Distance Query

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=21019

AC

143

Submit

262

Ratio

54.58%

时间&空间限制

  • Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others)
  • 描述

    The traffic network in a country consists of N cities (labeled with integers from 1 to N) and N-1 roads connecting the cities. There is a unique path between each pair of different cities, and we know the exact length of each road.

    Write a program that will, for each of the K given pairs of cities, find the length of the shortest and the length of the longest road on the path between the two cities.

    输入

    The first line of input contains an integer N, 2 ≤ N ≤ 100 000. Each of the following N-1 lines contains three integers A, B and C meaning that there is a road of length C between city A and city B.

    The length of each road will be a positive integer less than or equal to 1 000 000. The next line contains an integer K, 1 ≤ K ≤ 100 000. Each of the following K lines contains two different integers D and E --- the labels of the two cities constituting one query.

    输出

    Description

    The traffic network in a country consists of N cities (labeled with integers from 1 to N) and N-1 roads connecting the cities. There is a unique path between each pair of different cities, and we know the exact length of each road.

    Write a program that will, for each of the K given pairs of cities, find the length of the shortest and the length of the longest road on the path between the two cities.

    Input

    The first line of input contains an integer N, 2 ≤ N ≤ 100 000. Each of the following N-1 lines contains three integers A, B and C meaning that there is a road of length C between city A and city B.

    The length of each road will be a positive integer less than or equal to 1 000 000. The next line contains an integer K, 1 ≤ K ≤ 100 000. Each of the following K lines contains two different integers D and E --- the labels of the two cities constituting one query.

    Output

    Each of the K lines of output should contain two integers --- the lengths from the task description for the corresponding pair of the cities.

    Sample Input

    Sample #1
    5
    2 3 100
    4 3 200
    1 5 150
    1 3 50
    3
    2 4
    3 5
    1 2


    Sample #2
    7
    3 6 4
    1 7 1
    1 3 2
    1 2 6
    2 5 4
    2 4 4
    5
    6 4
    7 6
    1 2
    1 3
    3 5

    Sample Output

    Sample #1
    100 200
    50 150
    50 100


    Sample #2
    2 6
    1 4
    6 6
    2 2
    2 6

    Source

    样例输入

    Sample #1
    5
    2 3 100
    4 3 200
    1 5 150
    1 3 50
    3
    2 4
    3 5
    1 2


    Sample #2
    7
    3 6 4
    1 7 1
    1 3 2
    1 2 6
    2 5 4
    2 4 4
    5
    6 4
    7 6
    1 2
    1 3
    3 5

    样例输出

    Sample #1
    100 200
    50 150
    50 100


    Sample #2
    2 6
    1 4
    6 6
    2 2
    2 6

    提示

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部