-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathE1.cpp
59 lines (59 loc) · 1.13 KB
/
E1.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
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
vector <pair <int, int>> g[maxn];
int leaf[maxn];
int par[maxn];
void dfs(int x, int p) {
leaf[x] = 0;
int child = 0;
for(auto i : g[x]) {
if(i.first != p) {
par[i.first] = i.second;
dfs(i.first, x);
leaf[x] += leaf[i.first];
child += 1;
}
}
leaf[x] += (child == 0);
}
void solve() {
int n;
long long S;
scanf("%d %lld", &n, &S);
for(int i = 0; i <= n; i++) g[i].clear();
for(int i = 1; i < n; i++) {
int p, q, r;
scanf("%d %d %d", &p, &q, &r);
g[p].emplace_back(q, r);
g[q].emplace_back(p, r);
}
dfs(1, 0);
long long need = -S;
vector <long long> v;
for(int i = 2; i <= n; i++) {
need += 1LL * leaf[i] * par[i];
int cost = par[i];
while(cost > 0) {
v.push_back(1LL * leaf[i] * ((cost + 1) / 2));
cost /= 2;
}
}
sort(v.begin(), v.end(), greater <long long> ());
int ans = 0;
for(auto i : v) {
if(need > 0) {
need -= i;
ans += 1;
}
}
printf("%d\n", ans);
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
solve();
}
return 0;
}