我是靠谱客的博主 动人香氛,最近开发中收集的这篇文章主要介绍Codeforces 739B Alyona and a tree (树上差分+二分),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述


D. 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 ≤ n1 ≤ 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).



题目大意是给出一棵有根树,树上每个点、每条边都有一个权值。

现在给出“控制”的定义:对一个点u,设点v在其子树上,且 dis(u,v)av ,则称u控制v。

要求求出每个点控制了多少个点

模拟dfs过程,我们很容易发现dfs到点u时,其祖先节点到根的dis值都已经算出,且是单调递增,所以我们可以用二分或者倍增,在log的时间复杂度内找到深度最小的满足 dis(u,v)au 的点了。找到点v后,v到fa(u)上的每个点的答案都要增加1,显然用树上差分来实现,对ans[fa[v]]–,ans[fa[u]]++

一般写法。。。用id模拟每条路的节点编号,也用于二分

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 2e5 + 5;
typedef long long ll;
ll ans[maxn], path[maxn], a[maxn], pre[maxn], cnt[maxn];
struct node
{
    int to, len;
    node(){}
    node(int tt, int ll) : to(tt), len(ll){}
};
vector<node> p[maxn];
void dfs(int x, int id, int f, ll s)
{
    pre[id] = s;
    path[id] = x;
//    cout << 1 << endl;
    if(id > 1)
    {
        int l = 1, r = id, temp;
        while(l <= r)
        {
            int mid = (l+r)/2;
            if(a[x] >= s - pre[mid])
                temp = mid, r = mid - 1;
            else l = mid + 1;
        }
        cnt[path[temp-1]]--;
        cnt[path[id-1]]++;
    }
    for(int i = 0; i < p[x].size(); i++)
    {
        int to = p[x][i].to;
        int len = p[x][i].len;
        dfs(to, id+1, x,len+s);
    }
}
void dfs_ans(int x)
{
//    cout << 1 << endl;
    ans[x] = cnt[x];
    for(int i = 0; i < p[x].size(); i++)
    {
        int to = p[x][i].to;
        dfs_ans(to);
        ans[x] += ans[to];
    }
}
int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        for(int i = 1; i <= n; i++)
            scanf("%lld", &a[i]);
        int x, y;
        for(int i = 1; i < n; i++)
        {
            scanf("%d%d", &x, &y);
            p[x].push_back(node(i+1, y));
        }
        dfs(1, 1, 1, 0);
        dfs_ans(1);
        for(int i = 1; i <= n; i++)
        {
            printf("%lld%c", ans[i], i == n ? 'n' : ' ');
        }
    }
	return 0;
}

orz网上大神写法,low_bountd劲啊

用队列模拟一条路上的节点

#include<bits/stdc++.h>
//#define make_pair MP
using namespace std;

typedef __int64 ll;
typedef pair<ll, int> PII;
const int MAXN = 2e5+5;
vector<PII> G[MAXN], path;
ll dep[MAXN];
int sum[MAXN], a[MAXN];

void dfs(int u) {
    path.push_back(make_pair(dep[u], u));
    int it = lower_bound(path.begin(), path.end(), make_pair(dep[u]-a[u],-1))-path.begin()-1;  //对vec用二分函数
    if(it >= 0) sum[path[it].second]--;
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i].first, w = G[u][i].second;
        dep[v] = dep[u]+w;
        dfs(v);
        sum[u] += sum[v]+1;
    }
    path.pop_back();
}

int main() {
    memset(sum, 0, sizeof(sum));
    int n; cin >> n;
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for(int i = 2; i <= n; i++) {
        int t, w; scanf("%d%d", &t, &w);
        G[t].push_back(make_pair(i, w));
    }
    dep[1] = 0;
    dfs(1);
    for(int i = 1; i <= n; i++) printf("%d ", sum[i]);
    return 0;
}



最后

以上就是动人香氛为你收集整理的Codeforces 739B Alyona and a tree (树上差分+二分)的全部内容,希望文章能够帮你解决Codeforces 739B Alyona and a tree (树上差分+二分)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部