CodeForces 682C Alyona and the Tree(广搜 + 技巧)
方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<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(广搜 + 技巧)的更多相关文章
- CodeForces 682C Alyona and the Tree (树+dfs)
Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...
- 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 ...
- codeforces 682C Alyona and the Tree(DFS)
题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...
- 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
题目链接:http://codeforces.com/problemset/problem/682/C 分析:存图,用dfs跑一遍,详细见注释 1 #include<iostream> 2 ...
- Codeforces 682C Alyona and the Tree(树形DP)
题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...
- codeforces 682C Alyona and the Tree DFS
这个题就是在dfs的过程中记录到根的前缀和,以及前缀和的最小值 #include <cstdio> #include <iostream> #include <ctime ...
- CodeForces 682C Alyona and the Tree (树上DFS)
题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- LeetCode OJ 78. Subsets
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- Openjudge-计算概论(A)-求分数序列和
描述: 有一个分数序列 2/1,3/2,5/3,8/5,13/8,21/13,.... 求这个分数序列的前n项之和.输入输入有一行:正整数n.输出输出有一行:分数序列的和(浮点数,精确到小数点后4位) ...
- SQL函数学习(二):DATEADD() 函数
DATEADD() 函数在日期中添加或减去指定的时间间隔. 语法 DATEADD(datepart,number,date) date 参数是合法的日期表达式.number 是您希望添加的间隔数:对于 ...
- 常用的meta标签
<!-- 关键字,搜所引擎 SEO --> <meta http-equiv="keywords" content="关键字1,关键字2,...&quo ...
- javaScript中的一些知识
利用js动态生成table <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http ...
- HDU2544-最短路(最短路模版题目)
Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要 ...
- InvalidateRect只是增加重绘区域,在下次WM_PAINT的时候才生效
emWIN里面的无效重绘和windows很类似. WM_InvalidateArea()和WM_InvalidateRect()只重绘指定的区域,其他区域不会重绘,这样避免了闪烁,重绘发生在下次WM_ ...
- js实现url链接encode加密
function urlencode(clearString) { var output = ''; var x = 0; clearString = utf16to8(clearString.toS ...
- c语言_头文件
传统 C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <assert.h> //设定插入点 #include <ctyp ...
- win7下将dll文件的打开方式改回系统默认
打开注册表,定位到HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.dll把除OpenWit ...