Weak Pair
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 5985 Accepted Submission(s): 1683
Problem Description
You are given a rooted tree of N nodes, labeled from 1 to N. To the ith 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
1 2 3 1 2 1 2
Sample Output
1
Source
2016 ACM/ICPC Asia Regional Dalian Online
Recommend
wange2014 | We have carefully selected several similar problems for you: 6667 6666 6665 6664 6663
Statistic | Submit | Discuss | Note
题意:
weak pair的要求:
1.u是v的祖先(注意不一定是父亲)
2.val[u]*val[v] <=k;
问有多对?
分析:
val[u]<=k/val[v],对于当前节点来说,即祖先节点有多少满足小于 k/val[u],利用dfs的特性建立树状数组,一看代码就懂。