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

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

A Tree and Two Edges

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

요약
노드 n개와 간선 n+1개로 이루어진 연결 그래프가 주어질 때, 각 질의 쌍 사이의 단순 경로 개수를 구한다.
난이도

보통10점 중 6점

유형
그래프, 트리, BFS, 구현
정답자
아직 제출이 없습니다

문제

Given a connected simple graph (with at most one edge between any pair of nodes) with nn nodes and n+1n+1 edges (that's a tree with two extra edges), answer a list of queries: for two distinct nodes, how many simple paths are there between them? A simple path is a path that does not repeat nodes.

입력

The first line of input contains two integers nn (4≤n≤5×1044 \le n \le 5 \times 10^4) and qq (1≤q≤5×1041 \le q \le 5 \times 10^4), where nn is the number of nodes and qq is the number of queries. The nodes are numbered from 11 to nn.

Each of the next n+1n+1 lines contains two integers aa and bb (1≤a<b≤n1 \le a < b \le n) indicating that there is an edge in the graph between nodes aa and bb. All edges are distinct.

Each of the next qq lines contains two integers uu and vv (1≤u<v≤n1 \le u < v \le n). This is a query for the number of simple paths between nodes uu and vv.

출력

Output qq lines. On each line output a single integer, which is the number of simple paths between the query nodes. Output the answers to the queries in the order they appear in the input.

예제2

  1. 예제 1

    입력
    4 6
    1 2
    1 3
    1 4
    2 3
    2 4
    1 2
    1 3
    1 4
    2 3
    2 4
    3 4
    
    예상 출력
    3
    3
    3
    3
    3
    4
    
  2. 예제 2

    입력
    6 4
    1 2
    1 3
    1 6
    2 3
    3 4
    3 5
    4 5
    1 2
    1 3
    1 4
    1 6
    
    예상 출력
    2
    2
    4
    1