22142_TheMiddle

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

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

Pro.ID

22142

Title

The Middle

Title链接

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

AC

2

Submit

39

Ratio

5.13%

时间&空间限制

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

    Wind and Zero live in a country consisting of N cities. Wind lives in city X, and Zero lives in city Y. There is exactly one path between any two cities. One day, Wind and Zero want to meet each other. So they decide to meet at the middle of the path between city X and city Y. Now, I want you to tell me where it is.

    输入

    The first line of the input is a single number T, the number of the test cases of the input. Then T blocks each represent a single test case.

    The first line of each block contains one integer N ( 1 ≤ N ≤ 10000 ). Then N-1 lines follow, each one contains two integers a, b, which means there is an edge between city a and city b ( 1 ≤ a, bN ).

    Next line contains a integer M ( 1 ≤ M ≤ 100000 ), which is the number of queries.

    Then M lines follow, each one contains two integers x, y, which are the city numbers Wind and Zero live ( 1 ≤ x, yN ).

    输出

    Description

    Wind and Zero live in a country consisting of N cities. Wind lives in city X, and Zero lives in city Y. There is exactly one path between any two cities. One day, Wind and Zero want to meet each other. So they decide to meet at the middle of the path between city X and city Y. Now, I want you to tell me where it is.

    Input

    The first line of the input is a single number T, the number of the test cases of the input. Then T blocks each represent a single test case.

    The first line of each block contains one integer N ( 1 ≤ N ≤ 10000 ). Then N-1 lines follow, each one contains two integers a, b, which means there is an edge between city a and city b ( 1 ≤ a, bN ).

    Next line contains a integer M ( 1 ≤ M ≤ 100000 ), which is the number of queries.

    Then M lines follow, each one contains two integers x, y, which are the city numbers Wind and Zero live ( 1 ≤ x, yN ).

    Output

    For every query, if the middle in the path between city x and city y is located at a city, output the number of that city. If the middle is located at a edge, output two numbers, and the city nearer to city x should output first.

    Print a blank line after each test case.

    Sample Input

    1
    6
    1 2
    2 3
    2 4
    4 5
    6 4
    3
    5 3
    3 5
    6 5

    Sample Output

    4 2
    2 4
    4

    Source

    样例输入

    1
    6
    1 2
    2 3
    2 4
    4 5
    6 4
    3
    5 3
    3 5
    6 5

    样例输出

    4 2
    2 4
    4

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部