方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<0,就让它是0,0可以消除负数效应,让后面的点重新参与正常删除操作,这个方法的正确性不难证明,可以自己画一下图。而且还有比较方便的方法就是,记录不被删除的点,然后n-他们就可以了。

  代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define N 100010
#define INF 1e9
struct Node
{
int to,val;
};
vector<Node> vt[N];
int a[N],n,vis[N],del[N],fa[N],mark[N];
queue<int> que;
queue<int> q1;
void Delete(int u)
{
int len,v,now;
while(!q1.empty()) q1.pop();
q1.push(u);
del[u] = ;
while(!q1.empty())
{
now = q1.front();
q1.pop();
len = vt[now].size();
for(int i = ; i < len; i++)
{
v = vt[now][i].to;
if(!del[v] && fa[u] != v)
{
del[v] = ;
q1.push(v);
}
}
}
}
void bfs(int s)
{
que.push(s);
vis[s] = ;
int now,nxt,len,dis;
while(!que.empty())
{
now = que.front();
que.pop();
len = vt[now].size();
for(int i = ; i < len; i++)
{
nxt = vt[now][i].to;
dis = vt[now][i].val;
fa[nxt] = now;
if(vis[nxt] == INF)
{
vis[nxt] = vis[now] + dis;
if(vis[nxt] < ) vis[nxt] = ;
if(vis[nxt] > a[nxt])
{
Delete(nxt);
}
else que.push(nxt);
}
}
}
}
int main()
{
memset(del,,sizeof(del));
scanf("%d",&n);
for(int i = ; i <= n; i++)
{
scanf("%d",&a[i]);
vt[i].clear();
fa[i] = i;
vis[i] = INF;
}
int b,wei;
Node L,R;
for(int i = ; i <= n; i++)
{
scanf("%d%d",&b,&wei);
R.to = b; R.val = wei;
vt[i].push_back(R);
L.to = i; L.val = wei;
vt[b].push_back(L);
}
while(!que.empty()) que.pop();
bfs();
int ans = ;
for(int i = ; i <= n; i++)
{
if(del[i])
{
ans++;
}
}
printf("%d\n",ans);
return ;
}

CodeForces 682C Alyona and the Tree(广搜 + 技巧)的更多相关文章

  1. CodeForces 682C Alyona and the Tree (树+dfs)

    Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...

  2. XJOI3363 树3/Codeforces 682C Alyona and the Tree(dfs)

    Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly fou ...

  3. codeforces 682C Alyona and the Tree(DFS)

    题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...

  4. Codeforces 682C Alyona and the Tree (树上DFS+DP)

    题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...

  5. Codeforces 682C Alyona and the Tree

    题目链接:http://codeforces.com/problemset/problem/682/C 分析:存图,用dfs跑一遍,详细见注释 1 #include<iostream> 2 ...

  6. Codeforces 682C Alyona and the Tree(树形DP)

    题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...

  7. codeforces 682C Alyona and the Tree DFS

    这个题就是在dfs的过程中记录到根的前缀和,以及前缀和的最小值 #include <cstdio> #include <iostream> #include <ctime ...

  8. CodeForces 682C Alyona and the Tree (树上DFS)

    题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...

  9. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. [ An Ac a Day ^_^ ] HDU 1257 基础dp 最长上升子序列

    最近两天在迎新 看来只能接着水题了…… 新生培训的任务分配 作为一个有担当的学长 自觉去选了动态规划…… 然后我觉得我可以开始水动态规划了…… 今天水一发最长上升子序列…… kuangbin有nlog ...

  2. AFNetworking 关于JSON text did not start with array or object and option to allow fragments not set 错误

    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager]; [manager GET:@"http://www.baidu. ...

  3. css格式布局

    一.position:fixed 锁定位置(相对于浏览器的位置),例如有些网站的右下角的弹出窗口. 示例 : 二.position:absolute 1.外层没有position:absolute(或 ...

  4. 插件管理工具 Alcatraz

    Alcatraz 安装: https://github.com/alcatraz/Alcatraz Github官网链接 终端安装方法 mkdir -p ~/Library/Application\ ...

  5. hdu 5524 二叉树找规律,二进制相关

    input n 1<=n<=1e18 output 有n个结点的满二叉树有多少个不相同结点数的子树 做法:树有h=log2(n)层,最多有2h-2种(1除外),然后再n减去u重复的即可 # ...

  6. iOS导航栏主题

    主要是取得导航栏的appearance对象,操作它就设置导航栏的主题 UINavigationBar *navBar = [UINavigationBar appearance]; 常用主题设置 导航 ...

  7. 《JavaScript高级程序设计》读书笔记 ---数据类型

    ECMAScript 中有5 种简单数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number.String和Object——复杂数据类型,Object 本质上是由一组 ...

  8. ZOJ 1967 POJ 2570 Fiber Network

    枚举起点和公司,每次用DFS跑一遍图,预处理出所有的答案.询问的时候很快就能得到答案. #include<cstdio> #include<cmath> #include< ...

  9. Dell7040mt安装win7系统说明

    几天新买的Dell7040mt收到了,机器预装了win10系统,把win10作为开发平台,可能会有一些问题,所以改为win7,今天折腾了一天,终于把win7系统装上了.总结一下安装的步骤. 1 准备启 ...

  10. hdu_5762_Teacher Bo(鸽笼原理)

    题目链接:hdu_5762_Teacher Bo 题意: 给你n个点,问你能否找到两对点的曼哈顿距离相等 题解: 最开始看到这题,看数据以为要向nlogn的复杂度发展,结果经验误导了自己,我们仔细观察 ...