zoukankan      html  css  js  c++  java
  • CodeForces 682C Alyona and the Tree (树+dfs)

    Alyona and the Tree

    题目链接:

    http://acm.hust.edu.cn/vjudge/contest/121333#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 vsad 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

    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
    Output
    5

    题意:

    删除最少的叶节点,使得不存在祖先路径之和大于点权的点;

    题解:

    直接dfs判断每个点是否要被删除,找出所有不需要删除的点;
    WA:路径权和为负时应置零----因为判断的是所有祖先点到该点的路径.

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <set>
    #include <vector>
    #define LL long long
    #define eps 1e-8
    #define maxn 150000
    #define inf 0x3f3f3f3f
    #define IN freopen("in.txt","r",stdin);
    using namespace std;
    
    typedef pair<int,LL> pii;
    vector<pii> g[maxn];
    int n;
    LL num[maxn];
    bool vis[maxn];
    int ans;
    
    void dfs(int u, LL sum) {
        vis[u] = 1;
        if(sum <= num[u]) ans++;
        else return;
        int sz = g[u].size();
        for(int i=0; i<sz; i++) {
            if(vis[g[u][i].first]) continue;
            dfs(g[u][i].first, max(0LL,sum+g[u][i].second));
        }
    }
    
    int main(int argc, char const *argv[])
    {
        //IN;
    
        while(scanf("%d",&n) != EOF)
        {
            memset(vis, 0, sizeof(vis));
            for(int i=1; i<=n; i++) g[i].clear();
            for(int i=1; i<=n; i++) scanf("%I64d", &num[i]);
            for(int i=1; i<=n-1; i++) {
                int v;LL w; scanf("%d %I64d",&v,&w);
                g[v].push_back(make_pair(i+1,w));
                g[i+1].push_back(make_pair(v,w));
            }
    
            ans = 0LL;
            dfs(1, 0LL);
    
            printf("%d
    ", n-ans);
        }
    
        return 0;
    }
    
    
  • 相关阅读:
    [引用]SQLServer占CPU100%
    负能量程序员杂谈(2)- 管理中的情和义
    负能量程序员杂谈(1)-世界上最单纯的职业:程序员
    FLV文件格式官方规范详解
    rtmp官方标准规范详细解析
    万恶的KPI、新兴的OKR及让人纠结的程序员考核
    管理点滴(一)
    选拨管理者的一个必要条件
    团队管理的简单总结:少即是多,体力透支,负能量管理,自我进化团队,沟通
    我的2015计划,目标
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5690924.html
Copyright © 2011-2022 走看看