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 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 nodesu 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
題意:有一棵樹,N個結點,N-1條邊,每個結點有權值ai,如果存在一個點對,由結點和它的祖先組成,
並且它們相乘的結果<=K,那麼這個點對就稱爲weak,求有多少個這樣的點對。 思路:先將ai和K/ai離散化,然後找到根節點遍歷樹,每插入一個結點前,用樹狀數組求當前小於K/ai的結點 有多少個並加入結果,然後再將樹狀數組ai的位置+1,每次回溯前將樹狀數組ai的位置-1。注意ai可能爲0,所 以K/ai要處理一下。
|
HDU5877 Weak Pair(樹狀數組+dfs)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.