Elevated Rails

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

요약
세 섬에 있는 세 개의 트리가 주어질 때, 두 간선을 추가해 모든 섬을 연결한 뒤 두 정점 사이 경로에 포함될 수 있는 최대 정점 수를 묻는 질의에 답한다.
난이도

어려움10점 중 8점

유형
트리, DFS, 동적 계획법, 구현
정답자
아직 제출이 없습니다

문제

Brandon and Geoffry like trains! They have been tasked with building a network of rails connecting nn train stations. Each of the stations is on one of three islands, and each island has at least one station on it.

Brandon has been working hard to establish connections between stations that are on the same island. Specifically, Brandon has set up enough connections that there is exactly one way to take a train between two stations on the same island without visiting the same station more than once. However, it is not yet possible to take a train between two stations on different islands.

Geoffry, looking at Brandon’s train network so far, asks him several questions. Each question picks two stations which are currently on different islands and asks what the maximum number of stations a path between these two stations could take if Brandon added exactly two more connections so that it was possible to reach every station from every other station.

Brandon is too busy dealing with rail signals to think about how to connect stations on different islands, and defers all of Geoffry’s questions to you to answer.

입력

The first line of input contains two integers nn and qq (3≤n≤1053 ≤ n ≤ 10^5, 1≤q≤2⋅1051 ≤ q ≤ 2 \cdot 10^5), the number of train stations and the number of Geoffry’s questions.

The next n−3n - 3 lines each contain two integers xx and yy (1≤x<y≤n1 ≤ x < y ≤ n), indicating that stations xx and yy are connected by a rail that can go in both directions.

It is guaranteed that the current rail connections satisfy the conditions given above – the nn stations can be grouped on three islands such that two stations are reachable from each other if and only if they are on the same island, and there is a unique path between the two stations that does not repeat any stations.

The next qq lines each contain two integers aa and bb, asking for the maximum number of stations that could appear on a path between station aa and station bb. It is guaranteed station aa and station bb are on different islands.

Each of the above questions are independent from each other.

출력

Output qq lines, one per question. The output for each question should be a single integer, the maximum number of stations that could appear on a path between station aa and station bb.

예제1

  1. 예제 1

    입력
    12 3
    1 2
    2 3
    2 4
    5 6
    8 9
    9 10
    9 11
    7 8
    11 12
    2 5
    11 4
    7 6
    
    예상 출력
    9
    9
    10