307704: CF1399E1. Weights Division (easy version)

Memory Limit:256 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Weights Division (easy version)

题意翻译

## 题目描述 简单版本和困难版本是两个完全不同的问题,因此我们建议:两题的题面都要仔细阅读。 给定一棵以 $1$ 号点为根的带权有根树。 树是一个无环连通图。有根树有一个被称作根的特殊节点。在从根到节点 $v$ 的路径上,最后一个不同于 $v$ 的节点被称作节点 $v$ 的父亲节点。以节点 $v$ 为父亲的节点称为节点 $v$ 的儿子节点。若一个节点没有任何儿子,则称它为叶子节点。带权树上的边带有权值。 定义一条路径的权值为这条路径上所有边的权值之和。特别地,一条从某个节点到它自己的路径权值为 $0$。 你可以进行一系列的操作,操作零次或多次。对于每次操作,你可以选择任意一条边,将其权值除以 $2$(向下取整)。更正式地说,在每次操作中,你可以选择一条边 $i$,使得这条边的权值 $w_i$ 变成 $\lfloor \frac{w_i}{2} \rfloor$。 你的任务是找到最小操作数,以满足所有从根到叶子的路径的权值之和不超过 $S$。换句话说,如果设 $w(i,j)$ 为从节点 $i$ 到节点 $j$ 的路径的权值,那么你需要使得 $\sum_{v \in leaves} w(root,v) \leq S$,其中 $leaves$ 是所有叶子组成的集合。 你需要回答 $t$ 组独立的数据。 ## 输入格式 输入第一行包含一个整数 $t\;(1 \leq t \leq 2 \cdot 10^4)$,表示数据组数。 接着输入 $t$ 组数据。 每组数据的第一行包含两个整数 $n$ 和 $S\;(2 \leq n \leq 10^5;1 \leq S \leq 10^{16})$,分别表示树上节点数,和你需要满足的最大可能的权值和。 紧接着 $n-1$ 行描述树上的边。边 $i$ 会以三个整数 $v_i,u_i$ 和 $w_i\;(1 \leq v_i,u_i \leq n;1 \leq w_i \leq 10^6)$ 进行描述,其中 $v_i$ 和 $u_i$ 是连接边 $i$ 的两个端点,$w_i$ 是这条边的权值。 保证所有 $n$ 的和不超过 $10^5\;(\sum n \leq 10^5)$。 ## 输出格式 对于每组数据,输出一行答案,表示能满足所有从根到叶子的路径的权值之和不超过 $S$ 的最小操作数。

题目描述

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 ( $ w_i := \left\lfloor\frac{w_i}{2}\right\rfloor $ ). 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 $ \sum\limits_{v \in leaves} w(root, v) \le 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 \le t \le 2 \cdot 10^4 $ ) — the number of test cases. Then $ t $ test cases follow. The first line of the test case contains two integers $ n $ and $ S $ ( $ 2 \le n \le 10^5; 1 \le S \le 10^{16} $ ) — 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 $ v_i $ , $ u_i $ and $ w_i $ ( $ 1 \le v_i, u_i \le n; 1 \le w_i \le 10^6 $ ), where $ v_i $ and $ u_i $ are vertices the edge $ i $ connects and $ w_i $ is the weight of this edge. It is guaranteed that the sum of $ n $ does not exceed $ 10^5 $ ( $ \sum n \le 10^5 $ ).

输出格式


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 $ .

输入输出样例

输入样例 #1

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

输出样例 #1

0
8
3

Input

题意翻译

## 题目描述 简单版本和困难版本是两个完全不同的问题,因此我们建议:两题的题面都要仔细阅读。 给定一棵以 $1$ 号点为根的带权有根树。 树是一个无环连通图。有根树有一个被称作根的特殊节点。在从根到节点 $v$ 的路径上,最后一个不同于 $v$ 的节点被称作节点 $v$ 的父亲节点。以节点 $v$ 为父亲的节点称为节点 $v$ 的儿子节点。若一个节点没有任何儿子,则称它为叶子节点。带权树上的边带有权值。 定义一条路径的权值为这条路径上所有边的权值之和。特别地,一条从某个节点到它自己的路径权值为 $0$。 你可以进行一系列的操作,操作零次或多次。对于每次操作,你可以选择任意一条边,将其权值除以 $2$(向下取整)。更正式地说,在每次操作中,你可以选择一条边 $i$,使得这条边的权值 $w_i$ 变成 $\lfloor \frac{w_i}{2} \rfloor$。 你的任务是找到最小操作数,以满足所有从根到叶子的路径的权值之和不超过 $S$。换句话说,如果设 $w(i,j)$ 为从节点 $i$ 到节点 $j$ 的路径的权值,那么你需要使得 $\sum_{v \in leaves} w(root,v) \leq S$,其中 $leaves$ 是所有叶子组成的集合。 你需要回答 $t$ 组独立的数据。 ## 输入格式 输入第一行包含一个整数 $t\;(1 \leq t \leq 2 \cdot 10^4)$,表示数据组数。 接着输入 $t$ 组数据。 每组数据的第一行包含两个整数 $n$ 和 $S\;(2 \leq n \leq 10^5;1 \leq S \leq 10^{16})$,分别表示树上节点数,和你需要满足的最大可能的权值和。 紧接着 $n-1$ 行描述树上的边。边 $i$ 会以三个整数 $v_i,u_i$ 和 $w_i\;(1 \leq v_i,u_i \leq n;1 \leq w_i \leq 10^6)$ 进行描述,其中 $v_i$ 和 $u_i$ 是连接边 $i$ 的两个端点,$w_i$ 是这条边的权值。 保证所有 $n$ 的和不超过 $10^5\;(\sum n \leq 10^5)$。 ## 输出格式 对于每组数据,输出一行答案,表示能满足所有从根到叶子的路径的权值之和不超过 $S$ 的最小操作数。

加入题单

算法标签: