#P8805. [蓝桥杯 2022 国 B] 机房

    ID: 9720 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2022最近公共祖先 LCA前缀和蓝桥杯国赛

[蓝桥杯 2022 国 B] 机房

Problem Description

On this day, Xiaoming is studying in the computer lab.

He finds that there are a total of nn computers in the lab, numbered from 11 to nn. There are network cables connecting the computers. In total, there are n1n-1 cables connecting the nn computers, so that any two computers are connected either directly or indirectly.

Xiaoming notices that the time a computer needs to forward, send, or receive information depends on how many other computers it is directly connected to, while the transmission time on the cables can be ignored. For example, if a computer is directly connected to dd other computers by cables, then any information passing through this computer will be delayed by dd units of time (the sender and the receiver also produce such a delay; of course, if the sender and the receiver are the same computer, the delay is counted only once).

Xiaoming has a total of mm questions: if computer uiu_{i} sends information to computer viv_{i}, what is the minimum time for the information to travel from uiu_{i} to viv_{i}?

Input Format

The input has n+mn+m lines in total. The first line contains two positive integers n,mn, m.

The next n1n-1 lines each contain two positive integers x,yx, y, indicating that the two computers numbered xx and yy are directly connected by a cable.

The next mm lines each contain two positive integers ui,viu_{i}, v_{i}, indicating Xiaoming's ii-th question.

Output Format

Output mm lines in total. The ii-th line contains one positive integer, the answer to Xiaoming's ii-th question.

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

Hint

Sample Explanation

The delays of these four computers are 2,2,1,12,2,1,1, respectively.

For the first query, going from 22 to 33 needs to pass through 2,1,32,1,3, so the total time is 2+2+1=52+2+1=5. For the second query, going from 33 to 44 needs to pass through 3,1,2,43,1,2,4, so the total time is 1+2+2+1=61+2+2+1=6.

For the third query, going from 33 to 33 produces only one delay, so the time is 11.

Constraints and Notes

For 30%30\% of the testdata, n,m1000n, m \leq 1000.

For 100%100\% of the testdata, n,m105n, m \leq 10^5.

Lanqiao Cup 2022 National Contest B Group, Problem H.

Translated by ChatGPT 5