题目链接
You are given a weighted rooted tree, vertex 1 is the root of this tree.
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex v is the last different from v vertex on the path from the root to the vertex v. Children of vertex v are all vertices for which v is the parent. A vertex is a leaf if it has no children. The weighted tree is such a tree that each edge of this tree has some weight.
The weight of the path is the sum of edges weights on this path. The weight of the path from the vertex to itself is 0.
You can make a sequence of zero or more moves. On each move, you select an edge and divide its weight by 2 rounding down. More formally, during one move, you choose some edge i and divide its weight by 2 rounding down (wi:=⌊wi2⌋).
Your task is to find the minimum number of moves required to make the sum of weights of paths from the root to each leaf at most S. In other words, if w(i,j) is the weight of the path from the vertex i to the vertex j, then you have to make ∑ v ∈ l e a v e s w ( r o o t , v ) ≤ S \sum_{v\in leaves}w(root,v)≤S ∑v∈leavesw(root,v)≤S, where leaves is the list of all leaves.
You have to answer t independent test cases.
The first line of the input contains one integer t (1≤t≤2e4) — the number of test cases. Then t test cases follow.
The first line of the test case contains two integers n and S (2≤n≤1e5;1≤S≤1e16) — the number of vertices in the tree and the maximum possible sum of weights you have to obtain. The next n−1 lines describe edges of the tree. The edge i is described as three integers vi, ui and wi (1≤vi,ui≤n;1≤wi≤1e6), where vi and ui are vertices the edge i connects and wi is the weight of this edge.
It is guaranteed that the sum of n does not exceed 1e5 (∑n≤1e5).
For each test case, print the answer: the minimum number of moves required to make the sum of weights of paths from the root to each leaf at most S.
3
3 20
2 1 8
3 1 7
5 50
1 3 100
1 5 10
2 3 123
5 4 55
2 100
1 2 409
0
8
3
不去看题目要求的,首先我们肯定要存下每一条边的权值 w w w 和访问的次数 c n t cnt cnt,并求出不做操作的所有权值和,这些可以跑两遍 DFS 解决,下面考虑怎么操作最优:
不能发现每一次操作减少的就是 ( w − w / 2 ) ∗ c n t (w-w/2)*cnt (w−w/2)∗cnt,我们可以对这个进行排序,每次贪心取最大的就行了,操作完再把 ( w / 2 , c n t ) (w/2,cnt) (w/2,cnt) 放进去,用优先队列可以完美地实现上述操作,AC代码如下:
#include
using namespace std;
typedef long long ll;
const int N=1e5+5;
vector<int>G[N];
int vis1[N]={0},vis2[N]={0},ans;
ll cnt[N]={0},sum=0,k;
struct cmp{
bool operator()(pair<ll,ll>a,pair<ll,ll>b){
return (a.first-a.first/2)*a.second<(b.first-b.first/2)*b.second;
}
};
priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,cmp>q;
map<pair<int,int>,ll>W;
void dfs(int u){
vis1[u]=1;
if(G[u].size()==1&&u!=1){
cnt[u]=1;
return;
}
for(auto v:G[u]){
if(!vis1[v]){
dfs(v);
cnt[u]+=cnt[v];
}
}
}
void distribute(int u){
vis2[u]=1;
for(auto v:G[u]){
if(!vis2[v]){
q.push({W[{min(u,v),max(u,v)}],cnt[v]});
sum+=W[{min(u,v),max(u,v)}]*cnt[v];
distribute(v);
}
}
}
void init(int n){
sum=ans=0;
for(int i=0;i<=n;i++) G[i].clear(),cnt[i]=vis1[i]=vis2[i]=0;
while(!q.empty()) q.pop();
}
int main(){
int t,n,u,v,w;
cin>>t;
while(t--){
cin>>n>>k;
init(n);
for(int i=0;i<n-1;i++){
cin>>u>>v>>w;
G[u].push_back(v);
G[v].push_back(u);
W[{min(u,v),max(u,v)}]=w;
}
dfs(1);
distribute(1);
while(sum>k){
sum-=(q.top().first-q.top().first/2)*q.top().second;
q.push({q.top().first/2,q.top().second});
ans++;
q.pop();
}
cout<<ans<<endl;
}
return 0;
}