codeforces 682C Alyona and the Tree
来源:互联网 发布:上海巨人网络校园招聘 编辑:程序博客网 时间:2024/05/22 07:59
题目链接:
http://codeforces.com/problemset/problem/682/C
题意:
从祖先到当前的叶子节点,问你有几个满足是其祖先到叶子的权值的和小于等于当前的节点值的。
题解:
一个简单的DFS,但是,我们可以对这个问题进行简单的转变,将原来问题求去掉几个转变成满足几个,这道题目就会简单很多。
代码:
#include <cmath>#include <cstdio>#include <vector>#include <string>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define met(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3fconst int maxn = 1e5+10;typedef long long ll;vector<pair<int,ll> > v[maxn];int visited[maxn];int num[maxn];ll ans;void dfs(int x,ll sum){ visited[x]=1; if(sum<=num[x]) ans++; else return; for(int i=0;i<v[x].size();i++) { if(!visited[v[x][i].first]) { dfs(v[x][i].first,max((ll)0,sum+v[x][i].second)); } }}int main(){ int n; cin>>n; for(int i=1;i<=n;i++) cin>>num[i]; for(int i=2;i<=n;i++) { int a; ll b; cin>>a>>b; v[i].push_back(make_pair(a,b)); v[a].push_back(make_pair(i,b)); } met(visited,0); ans=0; dfs(1,0); cout<<n-ans<<endl;}
阅读全文
0 0
- Codeforces 682C. Alyona and the Tree
- codeforces 682C. Alyona and the Tree
- codeforces 682C Alyona and the Tree
- Codeforces 682C C. Alyona and the Tree (DFS)
- CodeForces 682C Alyona and the Tree 搜索
- CodeForces 682C - Alyona and the Tree(dfs)
- codeforces 682C Alyona and the Tree(DFS)
- Codeforces 682C Alyona and the Tree DP
- C. Alyona and the Tree
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree
- Codeforces Round #358 (Div. 2)C. Alyona and the Tree
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree
- Codeforces 682C Alyona and the Tree【Dfs+YY思维】好题
- Codeforces Round #358 (Div. 2)-C. Alyona and the Tree(树形dp)
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree (DFS)
- Codeforces Round #358 (Div. 2) C Alyona and the Tree(树形DP)
- Codeforces Round #358 (Div. 2) -- C. Alyona and the Tree (树上的DFS)
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree (二叉树+DFS)
- while (~scanf("%d%d",&m,&n)) 浅析
- poi.jar处理Excel错误Cannot get a text value from a numeric cell
- 【微信小程序】支付过程详解
- js正则表达式中test,exec,match方法的区别说明
- 如何备份SolidWorks网络版服务器
- codeforces 682C Alyona and the Tree
- equals重写
- QFileSystemWatcher使用整理
- nginx报错:nginx.service failed to run 'start-pre' task: No space left on device
- 栈-leetcode 496. Next Greater Element I
- (python)百练1000: A+B Problem
- SSM框架中,mybits.xml配置,映射器的注意点
- 【前端】Handlebars,requirejs使用入门
- 历史图纸导入PDM方案