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 ...
随机推荐
- chapter11_3 字符串缓冲
逐行地读取一个文件,典型的代码是: local buff= " " for line in io.lines() do buff = buff .. line .. "\ ...
- javascript中onSubmit="return xxx()"的问题
javascript中onSubmit="return xxx()"刚开始我是想不通为什么要加return在里面呢,后来想想onSubmit="flase"就不 ...
- Python基础篇-day4
本节目录: 1.字符编码 2.函数 2.1参数 2.2变量 2.3返回值 2.4递归 2.5 编程范式 2.6 高阶函数 *************************************** ...
- Android.mk简单分析
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_SRC_FILES := $ ...
- Struts2--课程笔记2
动态方法调用(在请求的时候,再明确具体的响应方法,配置的时候不明确): LoginAction类中有两个方法some和second 1. 动态方法的调用(修改常量struts.enable.Dynam ...
- pur-ftpd在ubuntu上的安装2(数据库管理)
1.安装mysql数据库支持的pure-ftpd apt-get install pure-ftpd-mysql 2.添加分组"ftpgroup",并且添加分组虚拟用户" ...
- QTP使用小技巧
1.创建action template. 当希望在每一个新建action时都增加一些头部说明,比如作者.创建日期.说明等,用action template 来实现最简单快捷. ...
- 安装 svn
rpm -qa subversion yum install subversion svnserve --version 创建仓库目录例如: mkdir /home/svn/game 创建项目 svn ...
- Ansible4:Ad-hoc与命令执行模块【转】
Ad-Hoc 是指ansible下临时执行的一条命令,并且不需要保存的命令,对于复杂的命令会使用playbook.Ad-hoc的执行依赖于模块,ansible官方提供了大量的模块. 如:command ...
- eclipse如何快速抽取样式(style)或者include
在视图模式上选中要抽取的模块,然后点击右键就可以抽取了