|
|
Weak PairTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1161 Accepted Submission(s): 392
Problem Description
You are given a
rooted
tree of
N
nodes, labeled from 1 to
N
. To the
i
th node a non-negative value
ai
is assigned.An
ordered
pair of nodes
(u,v)
is said to be
weak
if
(1) u is an ancestor of v (Note: In this problem a node u is not considered an ancestor of itself); (2) au×av≤k . Can you find the number of weak pairs in the tree?
Input
There are multiple cases in the data set.
The first line of input contains an integer T denoting number of test cases. For each case, the first line contains two space-separated integers, N and k , respectively. The second line contains N space-separated integers, denoting a1 to aN . Each of the subsequent lines contains two space-separated integers defining an edge connecting nodes u and v , where node u is the parent of node v . Constrains: 1≤N≤105 0≤ai≤109 0≤k≤1018
Output
For each test case, print a single integer on a single line denoting the number of weak pairs in the tree.
Sample Input
Sample Output
Source
题意:有一棵树,N个结点,N-1条边,每个结点有权值ai,如果存在一个点对,由结点和它的祖先组成,
并且它们相乘的结果<=K,那么这个点对就称为weak,求有多少个这样的点对。 思路:先将ai和K/ai离散化,然后找到根节点遍历树,每插入一个结点前,用树状数组求当前小于K/ai的结点 有多少个并加入结果,然后再将树状数组ai的位置+1,每次回溯前将树状数组ai的位置-1。注意ai可能为0,所 以K/ai要处理一下。
|
HDU5877 Weak Pair(树状数组+dfs)
最新推荐文章于 2020-12-12 01:10:54 发布
本文介绍了一种在给定树结构中查找弱对的算法。弱对定义为一棵树中的节点与其祖先组成的对,且该对节点权值的乘积不大于预设阈值K。文章详细阐述了算法的具体实现步骤,包括节点权值离散化处理、树状数组的应用等。


171

被折叠的 条评论
为什么被折叠?



