方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<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. poj 1411 Calling Extraterrestrial Intelligence Again

    题意:给你数m,a,b,假设有数p,q,满足p*q<=m同时a/b<=p/q<=1,求当p*q最大的p和q的值 方法:暴力枚举 -_-|| and 优化范围 我们可以注意到在某一个m ...

  2. idea 注册码

    生成地址:http://idea.lanyus.com/ ------------------------------------- IntelliJ IDEA 注册码 *.lanyus.com及*. ...

  3. poj2140(奇因数的个数)

    #include<stdio.h>int main(){ int n,s=0; scanf("%d",&n); for(int i=1;i<=n;i++) ...

  4. js数据转换

    javascript有如下数据类型的转换方法:一,转换成数字 xxx*1.0 转换成字符串 xxx+"" 二,从一个值中提取另一种类型的值,并完成转换工作. 1.提取字符串中的整数 ...

  5. css样式表及属性

    CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页. /*注释区域*/    此为注释语法 一.样式表 (一)样式表的分类 1.内联样式表 和HTML联合显示,控 ...

  6. iOS 计算两个坐标之间的距离

    //第一个坐标 CLLocation *before=[[CLLocation alloc] initWithLatitude:29.553968 longitude:106.538872]; //第 ...

  7. SCALA STEP BY STEP

    http://www.artima.com/scalazine/articles/steps.html http://hongjiang.info/scala/

  8. iOS中的项目新特性页面的处理

    一般项目中都会出现新特性页面,比如第一次使用应用的时候,或者在应用设置里查看新特性的时候会出现. 这里,选择新建一个专门处理项目新特性的控制器,来完成功能. 首先是 NewFeaturesViewCo ...

  9. Openlayer 3 的画线测量长度

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. (转)用JMX监测JVM的运行参数

    翻译自http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html  用JMX管理你的JVMJMX是Java管理扩展 ...