-
Notifications
You must be signed in to change notification settings - Fork 3
/
K.cpp
63 lines (60 loc) · 1.26 KB
/
K.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using ll = long long;
using namespace std;
const int maxn = 200010;
int n, q, dep[maxn];
struct Query {
int u, v;
ll ans;
} query[maxn];
vector<int> G[maxn];
vector<Query*> Q[maxn];
int fa[maxn];
int findfa(int son) {
return fa[son] == son ? son : fa[son] = findfa(fa[son]);
}
void dfs(int p, int par = -1) {
for (int t: G[p]) {
if (t == par) continue;
dep[t] = dep[p] + 1;
dfs(t, p);
fa[t] = p;
}
for (auto &que: Q[p]) {
if (que->v == p) {
swap(que->u, que->v);
}
if (!dep[que->v]) continue;
int lca = findfa(que->v);
ll L = dep[que->u] - dep[lca] + dep[que->v] - dep[lca] + 1;
que->ans = L * (L + 1) / 2 + n - L;
}
}
int main() {
memset(dep, -1, sizeof(dep));
scanf("%d%d", &n, &q);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
for (int i = 1; i <= n; i++) {
fa[i] = i;
}
for (int i = 1; i <= q; i++) {
scanf("%d%d", &query[i].u, &query[i].v);
Q[query[i].u].push_back(query + i);
Q[query[i].v].push_back(query + i);
}
dep[1] = 1;
dfs(1);
for (int i = 1; i <= q; i++) {
printf("%lld\n", query[i].ans);
}
return 0;
}