codeforces 682C Alyona and the Tree(DFS)
题目链接:http://codeforces.com/problemset/problem/682/C
题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被删去的点数。
思路:1为根节点,从1开始DFS遍历,记录距离dis为到祖宗节点的最大距离。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
int a[N],num[N],ans;
ll edge[N];
vector<int> G[N];
void dfs1(int x)
{
if(G[x].size()==0)
{
num[x]=1;
return;
}
for(int i=0;i<G[x].size();i++)
{
dfs1(G[x][i]);
num[x]+=num[G[x][i]];
}
num[x]++;
}
void dfs(int x,ll dis)
{
if(dis>a[x])
{
ans+=num[x];
return;
}
for(int i=0;i<G[x].size();i++)
dfs(G[x][i],max(dis+edge[G[x][i]],edge[G[x][i]]));//最大距离
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
for(int i=1;i<n;i++)
{
int v;
scanf("%d%I64d",&v,&edge[i+1]);
G[v].push_back(i+1);
}
dfs1(1);
dfs(1,0);
printf("%d\n",ans);
return 0;
}
codeforces 682C Alyona and the Tree(DFS)的更多相关文章
- CodeForces 682C Alyona and the Tree (树+dfs)
Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...
- 【CodeForces - 682C】Alyona and the Tree(dfs)
Alyona and the Tree Descriptions 小灵决定节食,于是去森林里摘了些苹果.在那里,她意外地发现了一棵神奇的有根树,它的根在节点 1 上,每个节点和每条边上都有一个数字. ...
- Codeforces 682C Alyona and the Tree (树上DFS+DP)
题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...
- CodeForces 682C Alyona and the Tree (树上DFS)
题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...
- Codeforces 682C Alyona and the Tree(树形DP)
题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...
- CodeForces 682C Alyona and the Tree(广搜 + 技巧)
方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<0,就让它是0,0可以 ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- codeforces 381 D Alyona and a tree(倍增)(前缀数组)
Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)
题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...
随机推荐
- VS2012解决方案的设置
用VS开发项目时,一个解决方案可以包含多个项目,在此我记录一下: 1.首先我新建一个Win32Demo的解决方案: 2.勾选"空项目": 3.新建完之后,会默认生成一个Win32D ...
- EXCEL计算数字、汉字、英文单元格的计数
1.数字COUNT(A1:A100)2.汉字{=SUMPRODUCT(IF(LEN(A1:A100)LENB(A1:A100),1,0)*1)}3.英文{=SUMPRODUCT(IF(ISTEXT(A ...
- CentOS 7 最小化安装的网络配置
默认的最小化安装CentOS 7系统以后,是没有ipconfig这个命令的,依赖于net-tools工具包. 一.nmtui 这是一个类似于图形化的命令(和setup类似) 通过这个组件窗口可以设置各 ...
- MVC 4 用Nuget安装组件后的常见错误
1,[A]System.Web.WebPages.Razor.Configuration.HostSection 无法强制转换为 [B]System.Web.WebPages.Razor.Config ...
- 基础笔记5(file)
file 可以是目录和文件(只是是java程序与系统的文件进行一种关联) File file1 = new File("f:/mytest", "test5.txt&qu ...
- redis 在32位系统安装以及使用
1.下载32位redis文件. http://pan.baidu.com/s/1eRIZPAE 我下载后的路径是:E:\Redis\Redis 里面的文件如图: 2.调取命令窗口 win+R,输入命 ...
- tfs 分支
集团-IT部张强 11:15:211.主干时刻处于稳定状态,随时可以发布.设专门人员对主干代码进行管理,普通开发人员只读. 2.为开发任务建立开发分支.常规的可以以小组为单位建立分支,较大的任务可以建 ...
- OneProxy的功能与限制
数据库中间件的核心目的之一就是尽可能透明的线性扩展数据库能力.其主要功能介绍参考(http://www.onexsoft.com/zh/oneproxy.html). 但是透明只是相对的,下面就简要 ...
- Creating Materials at runtime And Issue of Shader.Find()
Creating Materials at runtimehttp://forum.unity3d.com/threads/create-materials-at-runtime.72952/ //通 ...
- hdu ----3695 Computer Virus on Planet Pandora (ac自动机)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...