-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE_1_Weights_Division_easy_version.cpp
More file actions
83 lines (66 loc) · 1.46 KB
/
E_1_Weights_Division_easy_version.cpp
File metadata and controls
83 lines (66 loc) · 1.46 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
72
73
74
75
76
77
78
79
80
81
82
83
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
vector<pair<int, int>> adj[N];
int weight[N];
int dp[N];
int dfs(int node, int p) {
bool leaf = 1;
int ans = 0;
for(auto &j : adj[node]) if(j.first != p) {
leaf = 0;
int cnt = dfs(j.first, node);
ans += cnt;
dp[j.second] += cnt;
}
return ans + leaf;
}
void solve() {
int n, S;
cin >> n >> S;
for(int i=0;i<=n;i++) {
adj[i].clear();
dp[i] = 0;
}
for(int i=0;i<n-1;i++) {
int u, v, w;
cin >> u >> v >> weight[i];
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
dfs(1, -1);
set<pair<int, int>, greater<pair<int, int>>> st;
vector<int> wt(n-1);
for(int i=0;i<n-1;i++) {
wt[i] = weight[i];
}
int curr = 0;
for(int i=0;i<n-1;i++) {
st.insert({dp[i] * (wt[i] - (wt[i] / 2)), i});
curr += dp[i] * wt[i];
}
int ans = 0;
while(!st.empty() && curr > S) {
auto it = st.begin();
int sum = it->first;
int id = it->second;
st.erase(it);
curr -= sum;
ans++;
wt[id] /= 2;
st.insert({dp[id] * (wt[id] - (wt[id] / 2)), id});
}
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;
}