codeforces 682C. Alyona and the Tree
来源:互联网 发布:万网云新建数据库 编辑:程序博客网 时间:2024/06/06 18:50
codeforces 682C. Alyona and the Tree
题意
有一颗树,树上的点和边都有权值。要求删除树上若干节点,使得树上每个节点u到其子树上的节点v的
思路
类似与最大连续子段和的思想,对于每个节点是否要保留,在于是否有从根节点方向到以他为结尾的树链的权值比其点权大,有则删除该子树,无则保留该节点。
code
#include <vector>#include <iostream>#include <algorithm>using namespace std;#define MAXN (100000 + 5)#define INF (999999999)int n;int a[MAXN];int c[MAXN], dp[MAXN], ans;std::vector<pair<int, int> > v[MAXN];void CN (int rt) { if (v[rt].size() == 0) { c[rt] = 1; return; } c[rt] = 1; for (int i=0; i<v[rt].size(); i++) { CN(v[rt][i].first); c[rt] += c[v[rt][i].first]; }}void DP (int rt) { if (v[rt].size() == 0) { return ; } for (int i=0; i<v[rt].size(); i++) { pair<int, int> th = v[rt][i]; if (dp[rt] > 0) dp[th.first] = dp[rt] + th.second; else dp[th.first] = th.second; if (dp[th.first] <= a[th.first]) { DP(th.first); } else { ans += c[th.first]; } }}int main () { ios::sync_with_stdio(false); cin >> n; for (int i=1; i<=n; i++) { cin >> a[i]; } int tmpa, tmpb; for (int i=2; i<=n; i++) { cin >> tmpa >> tmpb; v[tmpa].push_back(make_pair(i, tmpb)); } ans = 0; CN(1); DP(1); cout << ans << endl; return 0;}
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)
- Linux 存储结构与磁盘划分
- 复制类的类方法
- 初识、理解适配器模式
- iOS开发从入门到精通--修改app应用的名称,添加AppIcon,LaunchImage
- poj 2236 Wireless Network 并查集
- codeforces 682C. Alyona and the Tree
- URL
- c# sqladapter 与sqldataReader
- C++ 标准模板库
- 支付宝支付
- 将mysql数据导入mongodb
- Web.xml配置详解之context-param
- GsonFormat的使用
- 把 Nginx 创建为 Windows 的一个服务