原文链接:http://codeforces.com/problemset/problem/1399/E1
Weights Division (easy version)
Easy and hard versions are actually different problems, so we advise you to read both statements carefully.
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∈leavesw(root,v)≤S, where leaves is the list of all leaves.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤2⋅104) — 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≤105;1≤S≤1016) — 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≤106), 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 105 (∑n≤105).
Output
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.
Example
input
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
output
0
8
3
题目大意
t t t组询问,每组给出节点数 n n n、权值和最大值 s s s以及一棵根节点为 1 1 1、带边权的树。
每次操作可以把一条边的权值 w i w_i wi变为 ⌊ w i 2 ⌋ \lfloor\frac{w_i}{2}\rfloor ⌊2wi⌋,要求使用最少的操作数使得所有叶子节点到根的路径权值之和 ≤ s \le s ≤s,输出最少的操作次数。
题解
又是类似模拟 + + +贪心的东西,这样我的模板复习大计什么时候能完成啊。
边权最大值为 1 0 6 ≤ 2 20 10^6\le 2^{20} 106≤220,也就是说边权一直右移的话最多移 20 20 20次就变成 0 0 0了,而 ∑ n ≤ 1 0 5 \sum n\le10^5 ∑n≤105所以就算我们把每一条边挨个右移 20 20 20次都不会超时。
于是就有了一个朴素的贪心,每次都选择最“赚”的边右移,检查当前路径权值之和是否 ≤ s \le s ≤s,否的话就继续操作。
很容易发现,叶子节点到根节点的路径是不变的,也就是说每条边在路径权值之和中重复的次数是一定的,我们可以先用一个 d f s dfs dfs统计出每条边在最终路径权值之和中重复的次数 t i m tim tim,初始的路径权值之和 t o t = ∑ ( t i m × w e i ) tot=\sum (tim\times wei) tot=∑(tim×wei)。
定义“赚”也很容易,每次右移边权的权值都会变为 w e i > > 1 wei>>1 wei>>1,那么总的路径权值之和减少值 v a l = t i m × [ w e i − ( w e i > > 1 ) ] val=tim\times[wei-(wei>>1)] val=tim×[wei−(wei>>1)], v a l val val越大对这条边进行操作就越“赚”。
接下来就只需要用一个以 v a l val val为比较值的优先队列来维护所有边了,每次弹出顶部最“赚”的边进行操作,路径权值之和 t o t − = v a l tot-=val tot−=val,检查总路径权值之和能否满足要求,不满足就将弹出的边的 w e i wei wei和 v a l val val进行修改再次放入队列里,直到满足为止。
代码
退役咸鱼含泪复习 l o n g l o n g 、 long long、 longlong、结构体 、 v e c t o r 、 p r i o r i t y _ q u e u e 、vector、priority\_queue 、vector、priority_queue
#include<bits/stdc++.h>
#define L long long
using namespace std;
const int M=1e5+5;
struct sd{int to,wei;};
struct edg{
int tim,wei;L val;
bool operator <(const edg &x)const{return x.val>val;}
};
int t,n,ans;
L tot,s;
vector<sd>mmp[M];
priority_queue<edg>dui;
bool vis[M];
void re()
{
ans=tot=0;
for(int i=1;i<=n;++i)vis[i]=0;
for(int i=1;i<=n;++i)mmp[i].erase(mmp[i].begin(),mmp[i].end());
for(;!dui.empty();dui.pop());
}
void in()
{
int a,b,c;
scanf("%d%I64d",&n,&s);
for(int i=1;i<n;++i)
{
scanf("%d%d%d",&a,&b,&c);
mmp[a].push_back((sd){b,c});
mmp[b].push_back((sd){a,c});
}
}
int dfs(int v)
{
L tmp,re=0;
int to,wei;
edg add;
vis[v]=1;
if(mmp[v].size()==1&&v!=1)return 1;
for(int i=mmp[v].size()-1;i>=0;--i)
{
if(vis[mmp[v][i].to])continue;
to=mmp[v][i].to,wei=mmp[v][i].wei;
tmp=dfs(to);
add.tim=tmp,add.wei=wei,add.val=tmp*(wei-wei/2);
dui.push(add);
tot+=tmp*wei;
re+=tmp;
}
return re;
}
void ac()
{
dfs(1);
for(edg tmp;tot>s;++ans)
{
tmp=dui.top();
tmp.wei>>=1,tmp.val=tmp.tim*1ll*(tmp.wei-tmp.wei/2);
tot-=dui.top().val;
dui.pop();
dui.push(tmp);
}
printf("%d\n",ans);
}
int main()
{
scanf("%d",&t);
for(int i=1;i<=t;++i)
in(),ac(),re();
}