Codeforces Round #358 (Div. 2) C. Alyona and the Tree DFS

C. Alyona and the Tree

链接:

http://www.codeforces.com/contest/682/problem/C

题意

给你一棵树,有点权有边权。

如果存在两个点,u,v。满足u存在v的子树中,(u,v)的之间的边权和大于a[u]的话,那么u点是不开心的。

你只能从叶子节点开始删除点,问你最少删除多少个点,可以使得这个树里面没有不开心的点。

题解:

题目中的树是有顺序的,所以直接从1号节点dfs就好了

如果要删除u点的话,那么显然u子树也得全部删去,所以直接dfs一波就完了。

代码:

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 const int maxn = 1e5 + 7;
 7 int a[maxn];
 8 vector<pair<int, int>> E[maxn];
 9 
10 int dfs(int x,int fa,long long dis)
11 {
12     if(dis > a[x])return 0;
13     int ans = 1;
14     for (int i = 0; i < E[x].size(); i++) {
15         if (E[x][i].first == fa)
16             continue;
17         ans += dfs(E[x][i].first, x, max(dis + E[x][i].second, 0LL));
18     }
19     return ans;
20 }
21 
22 int main()
23 {
24     int n;
25     cin >> n;
26     for (int i = 1; i <= n; i++)
27         cin >> a[i];
28     int p, c;
29     for (int i = 2; i <= n; i++) {
30         cin >> p >> c;
31         E[i].push_back(make_pair(p, c));
32         E[p].push_back(make_pair(i, c));
33     }
34     int ans = dfs(1, 0, 0);
35     cout << n - ans << endl;
36     return 0;
37 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值