Weak Pair
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3952 Accepted Submission(s): 1179
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?
(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
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
Recommend
wange2014
题意:
n个节点的树,节点的点权为ai,要求找出有多少个二元组(u,v)满足
1:u是v的祖先且u!=v
2:a[u]*a[v]<=K
思路(队友讲的比较好):
先把2转化一下:a[u] <=k/a[v] 因为都是整数所以不整除也没影响. 那么就是对每一个v找到他所有祖先里满足
上面那个a[u] <=k/a[v]不等式即可.
这个过程可以考虑为一个dfs过程,我们一边dfs一边查询即可。这里可以用一个树状数组,每遍历到一个就把他对应
树状数组的值+1,遍历v结点时,要查询他的祖先只需要查询有多少在k/a[v]
(其实就是快速查询当前有多少个点的值小于val的问题,BIT每个点的值对应BIT的下标)
但是这里A很大我们还是要离散化一下,有一个小问题就是如果仅仅把a[i]离散化了那么k/a[i]的值就会改变,所以我们这里考虑将k/a[i] 也一起加入进来离散化.(做除法记得判断a是否为0,为0的直接设为inf,不过数据好像没有?)
另外一个问题就是dfs过程中,每个点可能会受到其兄弟子树的影响,所以对于每个点为根的子树我们查询完后再给他清0,排除对其他子树的影响.
不怕被笑话,第一次做离散化的题, 知道离散化干嘛的, 但老是觉得离散化了会有毛病, 这题 其实把 a[x]/k也离散化很好。。 因为往树状数组里插的时候, 直接插原a[x], 查询的时候查 k/a[i]就好了,回溯的时候,一定要减去1, 这样就不会影响到子树了, 影响的只是这一条链上的值。。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const int maxn = 2e5 + 5;
int a[maxn], c[maxn], n, tot, Hash[maxn], in[maxn];
ll k, ans;
vector<int> v[maxn];
int lowbit(int x)
{
return x & -x;
}
void add(int x, int d)
{
while(x < maxn)
{
c[x] += d;
x += lowbit(x);
}
return ;
}
int sum(int x)
{
int res = 0;
while(x > 0)
{
res += c[x];
x -= lowbit(x);
}
return res;
}
void dfs(int x)
{
int tmp = lower_bound(Hash, Hash+tot, a[x] ? k/a[x] : INF) - Hash + 1;
ans += sum(tmp);
tmp = lower_bound(Hash, Hash+tot, a[x]) - Hash + 1; //因为是树状数组,所以整体左移
add(tmp, 1); // 把这个点更新到 tmp里
for(int i = 0; i < v[x].size(); ++i)
{
dfs(v[x][i]);
}
add(tmp, -1); //回溯完,把这个点减掉,因为祖先不能到别的子树上
return ;
}
int main()
{
int _;
cin >> _;
while(_--)
{
memset(c, 0, sizeof(c));
memset(in, 0, sizeof(in));
tot = 0;
scanf("%d%lld", &n, &k);
for(int i = 0; i <= n; i++)
v[i].clear();
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
Hash[tot++] = a[i];
if(a[i] == 0) Hash[tot++] = INF;
else Hash[tot++] = k/a[i];
}
sort(Hash, Hash+tot);
tot = unique(Hash, Hash+tot) - Hash; // 下标从1开始的话要+1
for(int i = 1; i < n; ++i)
{
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
in[y]++;
}
ans = 0;
for(int i = 1; i <= n; ++i)
{
if(!in[i])
{
dfs(i);
printf("%lld\n", ans);
break;
}
}
}
return 0;
}