Under construction

Still being built. Things may change

Airline

Time limit15sMemory limit512 MB

Summary
Given a tree of n airports, for each query adding a flight between x and y, count the airport pairs whose shortest distance decreases.
Level

Medium7 of 10

Topics
Tree, DFS, Math
Solved
No attempts yet

Problem

An airline operates regular flights that connect nn different airports. Each flight links two airports directly (without stopping at any other airport) and can be used in both directions. The flights are arranged so that for any starting airport ss and destination airport tt, there is exactly one sequence of flights between them that does not visit any airport more than once. The number of flights in this sequence is called the distance between ss and tt.

If the airline adds a flight between airports xx and yy, some pairs (s,t)(s, t) may get a shorter route. The more pairs affected, the more valuable the new connection is. The airline asks you to evaluate several candidate additions (x,y)(x, y) by this measure.

Input

The first line contains two integers nn and qq. The next n−1n-1 lines each contain two integers uiu_i and viv_i, a direct flight between airports uiu_i and viv_i. The last qq lines each contain two integers xix_i and yiy_i. In the ii-th scenario, a direct flight between xix_i and yiy_i is added to the original n−1n-1 flights.

Output

Output qq lines. The ii-th line contains the number of pairs (s,t)(s, t) with 1≤s<t≤n1 \le s < t \le n whose distance decreases when a direct flight between xix_i and yiy_i is added to the original n−1n-1 flights.

Constraints

  • 2≤n≤1062 \le n \le 10^6
  • 1≤q≤1051 \le q \le 10^5
  • 1≤ui≤n1 \le u_i \le n; 1≤vi≤n1 \le v_i \le n; ui≠viu_i \ne v_i
  • 1≤xi≤n1 \le x_i \le n; 1≤yi≤n1 \le y_i \le n; xi≠yix_i \ne y_i
  • ∑i=1qdi≤107\sum_{i=1}^{q} d_i \le 10^7, where did_i is the distance between xix_i and yiy_i in the original flight network.

Examples1

  1. Example 1

    Input
    8 2
    1 5
    5 2
    7 3
    3 8
    6 4
    4 5
    6 3
    5 7
    2 6
    
    Expected output
    10
    4