Codeforces E. Alyona and a tree(二分树上差分)

题目描述:

Alyona and a tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex i she wrote ai. Moreover, the girl wrote a positive integer to every edge of the tree (possibly, different integers on different edges).

Let's define dist(v, u) as the sum of the integers written on the edges of the simple path from v to u.

The vertex v controls the vertex u (v ≠ u) if and only if u is in the subtree of v and dist(v, u) ≤ au.

Alyona wants to settle in some vertex. In order to do this, she wants to know for each vertex v what is the number of vertices u such that vcontrols u.
Input

The first line contains single integer n (1 ≤ n ≤ 2·105).

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the integers written in the vertices.

The next (n - 1) lines contain two integers each. The i-th of these lines contains integers pi and wi (1 ≤ pi ≤ n, 1 ≤ wi ≤ 109) — the parent of the (i + 1)-th vertex in the tree and the number written on the edge between pi and (i + 1).

It is guaranteed that the given graph is a tree.
Output

Print n integers — the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.
Examples
input

5
2 5 1 4 6
1 7
1 1
3 5
3 6

output

1 0 1 0 0

input

5
9 7 8 6 5
1 1
2 1
3 1
4 1

output

4 3 2 1 0

Note

In the example test case the vertex 1 controls the vertex 3, the vertex 3 controls the vertex 5 (note that is doesn't mean the vertex 1controls the vertex 5).

思路:

这道题是说,给一棵树,树上的每个节点都有一个a[i]值,每条边上都有权重。如果对一个节点来说,他到子树下面某个节点的距离(权值和)小于等于那个节点的a[i]值,那它就能控制这个节点。那么现在对于每个节点,看它能控制多少个节点。

如果我们先dfs一下这棵树,就可以求出来所有节点的深度(带权值)。然后怎么办?难道要看结点1,下面的结点有几个满足被控制条件,结点2,看下面有几个满足被控制的条件...?想想时间复杂度就很高,而且该用什么数据结构实现呢(我是蒟蒻我不知道)

那转换一下关注对象,上面我们看的是对每个节点看他能控制那些节点,不如这样:对于每个节点我们看他能被那些节点控制。具体的,对节点u,如果p(p是u的祖先节点)满足(depth[u]-depth[p]leq a[u]),那u就能被p控制。又发现这样一个事实,树上从上往下深度是递增的,意味着如果p满足了控制u的条件,那么p之下,u之上的节点都能控制u。因为到u的距离小于p到u的距离,从而小于a[u]。单增序列,我们只要找到遍历路径上第一个满足控制u的条件的节点就行了。这时,我们可以建一个(vector)来存储路径,路径是边,第一个属性是这条边到达点深度,第二个属性是到达点的标号。由于序列的单调性,使用lower_bound即可求出p节点来。

现在就是要把p节点下u节点上的节点的控制个数加一了,对于区间加法,我们可以考虑差分数组,在这里,也就是树上差分。具体的,将p节点父亲的控制数减一(如果找得到的话),将u节点的父结点控制数加u节点的控制数再加一。每个节点在(dfs)回溯时都做类似处理,到达p点就会得到正确的答案。

注意的是及时更新路径,包括加入新的边和弹出已返回的边(才知道原来(vector)有个pop_back()函数,真方便)。

代码:

#include <iostream>
#include <vector>
#define max_n 200005
using namespace std;
typedef pair<int,long long> PIL;
typedef pair<long long,int> PLI;
vector<PIL> edge[max_n];
int ans[max_n];
int n;
int a[max_n];
vector<PLI> path;
long long depth[max_n];
void dfs(int s)
{
    /*cout << "s " << s << endl;
    cout << "path" << path.size() << endl;
    for(int i = 0;i<path.size();i++)
    {
        cout << path[i].first << " " << path[i].second << endl;
    }*/
    int node = lower_bound(path.begin(),path.end(),PLI(depth[s]-a[s],-1))-path.begin()-1;
    //cout << "node " << node << endl;
    if(node>=0) ans[path[node].second]--;
    path.push_back(PLI(depth[s],s));
    for(int i = 0;i<edge[s].size();i++)
    {
        int v = edge[s][i].first;
        long long w = edge[s][i].second;
        depth[v] = depth[s]+w;
        dfs(v);
        ans[s] += ans[v]+1;
    }
    path.pop_back();
}
int main()
{
    cin >> n;
    for(int i = 1;i<=n;i++)
    {
        cin >> a[i];
    }
    int v;
    long long w;
    for(int i = 1;i<n;i++)
    {
        cin >> v >> w;
        edge[v].push_back(PIL(i+1,w));
    }
    dfs(1);
    for(int i = 1;i<=n;i++)
    {
        cout << ans[i] << " ";
    }
    cout << endl;
    return 0;
}

参考文章:

qscqesze,Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和,https://www.cnblogs.com/qscqesze/p/6103445.html(是大佬,不做过多解释)

键盘里的青春,Codeforces 739B Alyona and a tree (树上差分+二分),https://blog.csdn.net/qq_34374664/article/details/70246427(为什么又是他?别问,问就是带佬)

原文地址:https://www.cnblogs.com/zhanhonhao/p/11299459.html