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

    C. Alyona and the Tree
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 udist(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.

    Example
    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
    Note

    The following image represents possible process of removing leaves from the tree:

    思路:dfs一波,找到sad的点,去掉sad为根的树;

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define mod 1000000007
    #define esp 0.00000000001
    const int N=1e5+10,M=1e6+10,inf=1e9+7;
    ll a[N];
    vector<pair<int,int > >edge[N];
    int getnum(int u)
    {
        int ans=0;
        for(int i=0;i<edge[u].size();i++)
        ans+=getnum(edge[u][i].first);
        return ans+1;
    }
    int dfs(int u,ll dis)
    {
        int ans=0;
        for(int i=0;i<edge[u].size();i++)
        {
            if(a[edge[u][i].first]<dis+edge[u][i].second)
            ans+=getnum(edge[u][i].first);
            else
            ans+=dfs(edge[u][i].first,max(dis+edge[u][i].second,0LL));
        }
        return ans;
    }
    int main()
    {
        int x,y,z,i,t;
        scanf("%d",&x);
        for(i=1;i<=x;i++)
        scanf("%d",&a[i]);
        for(i=1;i<x;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            edge[u].push_back(make_pair(i+1,v));
        }
        int ans=dfs(1,0);
        printf("%d
    ",ans);
        return 0;
    }
    C. Alyona and the Tree
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 udist(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.

    Example
    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
    Note

    The following image represents possible process of removing leaves from the tree:

  • 相关阅读:
    (1)定义一个接口Compute含有一个方法int computer(int n,int m); (2)设计四个类分别实现此接口,完成+-*/运算 (3)设计一个类UseCompute,含有方法: public void useCom(Compute com, int one, int two) (4)设计一个测试类
    定义一个“点”(Point)类用来表示三维空间中的点(有三个坐标)。要求如下: (1)可以生成具有特定坐标的点对象。 (2)提供可以设置三个坐标的方法。 (3)提供可以计算该“点”距原点距离平方的方法。 (4)编写主类程序验证。
    编写一个Java程序,计算半径为3.0的圆周长和面积并输出结果
    Java泛型详解
    面向对象
    随机数
    随机数的产生
    随机数
    matplotlib 绘图实例01:正弦余弦曲线
    第05课 Linux命令初探(一)
  • 原文地址:https://www.cnblogs.com/jhz033/p/5597715.html
Copyright © 2011-2022 走看看