-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathF_Tree_TREE.cpp
More file actions
71 lines (56 loc) · 1.18 KB
/
F_Tree_TREE.cpp
File metadata and controls
71 lines (56 loc) · 1.18 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
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 5;
vector<int> adj[N];
int sz[N];
int dp[N];
void dfs(int node, int p, int k) {
sz[node] = 1;
dp[node] = 0;
for(auto &j : adj[node]) if(j != p) {
dfs(j, node, k);
sz[node] += sz[j];
dp[node] += dp[j];
}
if(sz[node] >= k) dp[node] += 1;
}
void dfs2(int node, int p, int k, int &ans) {
ans += dp[node];
for(auto &j : adj[node]) if(j != p) {
bool p_sz = (sz[j] >= k);
bool n_sz = (sz[node] - sz[j] >= k);
sz[j] = sz[node];
dp[j] = dp[node] - p_sz + n_sz;
dfs2(j, node, k, ans);
}
}
void solve() {
int n, k;
cin >> n >> k;
for(int i=0;i<=n;i++) {
dp[i] = 0;
adj[i].clear();
}
for(int i=0;i<n-1;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, -1, k);
int ans = 0;
dfs2(1, -1, k, ans);
cout << ans << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _;
cin >> _;
while (_-->0) {
solve();
}
return 0;
}