Codeforces 682C Alyona and the Tree(树形DP)
题目大概说给一棵点有权、边也有权的树。一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值。现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点。
一开始题目看错了,以为说的是v到u路径上的边权和小于v的权值,然后想出了个解法:从根开始DFS,找高兴的结点,递归过程中在set插入各个祖先结权值,递归返回时从set中删除,而如果set里面最小的元素小于当前结点的路径和那么这个结点就不能要直接return,另外还用到一个简单的数学原理——两个数同时加上相同的数其大小关系不变。。时间复杂度O(nlogn)。
然后写好后才发现读错题。而事实上这题反而更容易。。同样也是从根开始DFS,遇到不高兴的就不往下DFS了,而判断是否高兴就用到个简单的DP了:
- dp[u]表示祖先到u结点中的最大边权和
- 由于边权可以为负,所以转移就是dp[v]=max(dp[u]+weight(u,v),weight(u,v))
- 而u结点不高兴,当且仅当d[u]>weight(u)
另外可以不用long long,这个是没问题的。。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 111111
struct Edge{
int v,w,next;
}edge[MAXN<<];
int NE,head[MAXN];
void addEdge(int u,int v,int w){
edge[NE].v=v; edge[NE].w=w; edge[NE].next=head[u];
head[u]=NE++;
} int val[MAXN],ans;
int d[MAXN];
void dfs(int u){
if(u!= && d[u]>val[u]){
return;
}
++ans;
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
d[v]=max(d[u]+edge[i].w,edge[i].w);
dfs(v);
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=; i<=n; ++i){
scanf("%d",val+i);
}
NE=;
memset(head,-,sizeof(head));
int a,b;
for(int i=; i<=n; ++i){
scanf("%d%d",&a,&b);
addEdge(a,i,b);
}
dfs();
printf("%d",n-ans);
return ;
}
Codeforces 682C Alyona and the Tree(树形DP)的更多相关文章
- 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+DP)
题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...
- 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
题目链接:http://codeforces.com/problemset/problem/682/C 分析:存图,用dfs跑一遍,详细见注释 1 #include<iostream> 2 ...
- codeforces 682C Alyona and the Tree DFS
这个题就是在dfs的过程中记录到根的前缀和,以及前缀和的最小值 #include <cstdio> #include <iostream> #include <ctime ...
- CodeForces 682C Alyona and the Tree(广搜 + 技巧)
方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<0,就让它是0,0可以 ...
- 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 ...
随机推荐
- 项目之solr全文搜索工具之创建项目索引库
以创建项目baotao core为例 1. 在example目录下创建baotao-solr文件夹: 2. 将./solr下的solr.xml拷贝到baotao-solr目录下: 3. 在bao ...
- IOS管理文件和目录NSFileManager
1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...
- hdu 1860统计字符
本来是想用map写的,但是map里面会自动按字典序升序排序导致wa了一把,供 #include<time.h> #include <cstdio> #include <i ...
- 体系结构设计MVC
体系结构设计MVC(viso图)GIT: https://coding.net/u/lklzjh/p/travel/git/blob/master/%E4%BD%93%E7%B3%BB%E7%BB%9 ...
- C#调用C++DLL的小总结5---和C++的DLL的联合调试
http://fpcfjf.blog.163.com/blog/static/5546979320134922938373/ http://blog.csdn.net/jiangxinyu/artic ...
- Android Stutio -- 编译报错: Error:File path too long on Windows, keep below 240
原文:http://blog.csdn.net/qq_28195645/article/details/51556975 目录太长,解决办法: 1.将整个project移到更外层的目录,直至没有报错, ...
- linux 普通用户切换成root免密码
[root@ok ~]# vim /etc/pam.d/su 下面是/etc/pam.d/su文件的内容 #%PAM-1.0 auth sufficient pam_rootok.so # Uncom ...
- EF – 5.DbSet与DbContext,数据更新奥秘
5.6.4 <DbSet与DbContext> 介绍DbSet与DbContext中的核心属性及重要方法. 5.6.5 <数据更新的奥秘> 这一讲极为重要,因为它揭示出了En ...
- jQuery – 6.选择器
1. 属性过滤选择器: 1. $("div[id]")选取有id属性的<div> 2. $("div[title=test]")选取title属性为 ...
- C#关键字ref和out
using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Di ...