Codeforces Round #358 (Div. 2) C. Alyona and the Tree

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

链接 http://codeforces.com/contest/682/problem/C

题意

给一棵树,每个节点和每条边都对应一个数值。两个节点间的距离为边上的权值之和。对于某个点来说,如果他到他的某个子节点的距离大于子节点上的数值,那么他是sad的。问最少去掉多少个节点,能使所有节点都不sad。

思路

dfs,贪心处理使当前路径之和最大,如果到达某节点时路径和大于点上的数值,那么这个点和这个点所有的子节点都需要去掉。dfs函数return当前节点及其子节点还未删去的数目。

代码

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
struct point
{
    vector<int> son;
    long long n,d;
}p[100010];
int ans;
int dfs (int k,long long sum)
{
    int l=p[k].son.size(),n=0;
    for (int i=0;i<l;i++)
    {
        n+=dfs(p[k].son[i],max(sum+p[k].d,(long long) 0));
    }
    if (sum+p[k].d>p[k].n)
    {
        ans+=1+n;
        return 0;
    }
    else
    {
        return n+1;
    }
}



int main()
{
    int n,t;
    long long c;
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%I64d",&p[i].n);
    for (int i=2;i<=n;i++)
    {
        scanf("%d %I64d",&t,&c);
        p[t].son.push_back(i);
        p[i].d=c;
    }
    dfs(1,0);
    printf("%d",ans);
}

PS

现场理解错题意了。。以为是去掉边。。好蠢。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值