zoukankan      html  css  js  c++  java
  • Codeforces Round #358 (Div. 2) C. Alyona and the Tree 水题

    C. Alyona and the Tree

    题目连接:

    http://www.codeforces.com/contest/682/problem/C

    Description

    Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly found a magic rooted tree with root in the vertex 1, every vertex and every edge of which has a number written on.

    The girl noticed that some of the tree's vertices are sad, so she decided to play with them. Let's call vertex v sad if there is a vertex u in subtree of vertex v such that dist(v, u) > au, where au is the number written on vertex u, dist(v, u) is the sum of the numbers written on the edges on the path from v to u.

    Leaves of a tree are vertices connected to a single vertex by a single edge, but the root of a tree is a leaf if and only if the tree consists of a single vertex — root.

    Thus Alyona decided to remove some of tree leaves until there will be no any sad vertex left in the tree. What is the minimum number of leaves Alyona needs to remove?

    Input

    In the first line of the input integer n (1 ≤ n ≤ 105) is given — the number of vertices in the tree.

    In the second line the sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 109) is given, where ai is the number written on vertex i.

    The next n - 1 lines describe tree edges: ith of them consists of two integers pi and ci (1 ≤ pi ≤ n,  - 109 ≤ ci ≤ 109), meaning that there is an edge connecting vertices i + 1 and pi with number ci written on it.

    Output

    Print the only integer — the minimum number of leaves Alyona needs to remove such that there will be no any sad vertex left in the tree.

    Sample Input

    9
    88 22 83 14 95 91 98 53 11
    3 24
    7 -8
    1 67
    1 64
    9 65
    5 12
    6 -80
    3 8

    Sample Output

    5

    Hint

    题意

    给你一棵树,有点权有边权。

    如果存在两个点,u,v。满足u存在v的子树中,(u,v)的之间的边权和大于a[u]的话,那么u点是不开心的。

    你只能从叶子节点开始删除点,问你最少删除多少个点,可以使得这个树里面没有不开心的点。

    题解:

    题目中的树是有顺序的,所以直接从1号节点dfs就好了

    如果要删除u点的话,那么显然u子树也得全部删去,所以直接dfs一波就完了。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5+6;
    vector<pair<int,int> >E[maxn];
    int a[maxn];
    int dfs(int x,int fa,long long dis)
    {
        if(dis>a[x])return 0;
        long long ans = 1;
        for(int i=0;i<E[x].size();i++)
        {
            if(E[x][i].first==fa)continue;
            ans+=dfs(E[x][i].first,x,max(dis+E[x][i].second,0LL));
        }
        return ans;
    }
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        int p,c;
        for(int i=1;i<=n-1;i++)
        {
            scanf("%d%d",&p,&c);
            E[i+1].push_back(make_pair(p,c));
            E[p].push_back(make_pair(i+1,c));
        }
        int ans = dfs(1,0,0);
        cout<<n-ans<<endl;
    }
  • 相关阅读:
    cocos2d-js 写日志log 查看日志log Android调试查看log
    嵌入式开发之hi3519---网络不通问题rmii
    嵌入式开发之视频压缩比---h264、mjpeg、mpeg4
    c、c++---linux上的GetTickCount函数
    嵌入式开发之hi3516---GV7601 SPI通信问题
    嵌入式开发之hisilicon---hi3536 处理器简介
    嵌入式开发之zynq---Zynq PS侧I2C驱动架构
    嵌入式开发值zynq---zynq中tlv320aic23b spi的驱动移植
    jumpserver 3.2修改排序规则
    haproxy 非常完整的配置
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5595711.html
Copyright © 2011-2022 走看看