zoukankan      html  css  js  c++  java
  • 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 thatv controls 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).

    分析:对每一个点,如果他与祖先的距离小于自身权值,则对祖先贡献+1,问最后所有点的答案数;

       如果这个点对"他自己的祖先"的祖先有贡献,那么显然对"他自己的祖先"也是有贡献的;

       所以考虑倍增(二分)求出这个点p能到达最远祖先q的地方,那么ans[p父亲~q]++;

       考虑前缀和,即ans[p父亲]++,ans[q父亲]--,最后dfs处理前缀和即可;

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <unordered_map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    #define intxt freopen("in.txt","r",stdin)
    const int maxn=2e5+10;
    using namespace std;
    int gcd(int p,int q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,fa[20][maxn],ans[maxn],dep[maxn],a[maxn];
    ll w[20][maxn];
    vector<pii>e[maxn];
    void dfs(int p)
    {
        for(int i=1;fa[i-1][fa[i-1][p]];i++)
        {
            fa[i][p]=fa[i-1][fa[i-1][p]];
            w[i][p]=w[i-1][p]+w[i-1][fa[i-1][p]];
        }
        for(pii x:e[p])
        {
            int to=x.fi,q=x.se;
            if(to==fa[0][p])continue;
            dep[to]=dep[p]+1;
            fa[0][to]=p;
            w[0][to]=q;
            dfs(to);
        }
    }
    void dfs1(int now,int pre)
    {
        for(pii x:e[now])
        {
            if(x.fi!=pre)
            {
                dfs1(x.fi,now);
                ans[now]+=ans[x.fi];
            }
        }
    }
    int query(int p,int pos)
    {
        for(int i=19;i>=0;i--)
            if(w[i][pos]&&w[i][pos]<=p)p-=w[i][pos],pos=fa[i][pos];
        return pos;
    }
    int main()
    {
        int i,j;
        dep[1]=1;
        scanf("%d",&n);
        rep(i,1,n)scanf("%d",&a[i]);
        rep(i,1,n-1)scanf("%d%d",&j,&k),e[j].pb(mp(i+1,k)),e[i+1].pb(mp(j,k));
        dfs(1);
        rep(i,1,n)
        {
            int q=query(a[i],i);
            ans[fa[0][i]]++;
            ans[fa[0][q]]--;
        }
        dfs1(1,0);
        rep(i,1,n)printf("%d ",ans[i]);
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    Android popupwindow和dialog监听返回键
    Android开发常用资料传送门
    Android 自己搭建一个直播系统吧
    js 时间戳转换成几分钟前,几小时前,几天前
    Android 热补丁动态修复框架小结
    【活动】参加葡萄城控件主办的“谁是报表达人”知识评测活动,赢取iPad Mini2团队
    上周热点回顾(3.24-3.30)团队
    C#正则表达式引发的CPU跑高问题以及解决方法团队
    上周热点回顾(3.17-3.23)团队
    实际遭遇GC回收造成的Web服务器CPU跑高团队
  • 原文地址:https://www.cnblogs.com/dyzll/p/6102928.html
Copyright © 2011-2022 走看看