-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree,TREE!!!.cpp
More file actions
60 lines (49 loc) · 1.08 KB
/
Tree,TREE!!!.cpp
File metadata and controls
60 lines (49 loc) · 1.08 KB
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
#include <bits/stdc++.h>
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
using u128 = unsigned __int128;
using i128 = __int128;
void solve() {
int n, k;
std::cin >> n >> k;
i64 ans = 0;
std::vector<std::vector<int>> adj(n);
for (int i = 1; i < n; i++) {
int u, v;
std::cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
std::vector<int> siz(n);
[&](this auto &&self, int x, int p) -> void {
siz[x] = 1;
for (auto y : adj[x]) {
if (y == p) {
continue;
}
self(y, x);
siz[x] += siz[y];
}
if (siz[x] >= k) {
ans += n - siz[x];
}
if (n - siz[x] >= k) {
ans += siz[x];
}
} (0, -1);
ans += n;
std::cout << ans << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}