zl程序教程

您现在的位置是:首页 >  其它

当前栏目

【Codeforces 682C】Alyona and the Tree

The and Codeforces Tree
2023-09-14 09:03:44 时间

【链接】 我是链接,点我呀:)
【题意】

题意

【题解】

设dis[v]表示v以上的点到达这个点的最大权值(肯定是它的祖先中的某个点到这个点) 类似于最大连续累加和 当往下走(x,y)这条边的时候,设其权值为w,以及到目前为止走过的最大权值和cur 如果cur<0,那么就不要之前的路径了,直接加上cur。 否则连着之前的路径加上去就好。 这样就能贪心地求出来dis[v]了 如果dis[x]>a[x]那么它以及它下面的所有节点都要删掉(因为要从叶子节点开始删) 如果dis[x]<=a[x]那么就可以保留这个点了。 用总数减去最多可以保留的点的个数。就是最少需要删掉的个数了。

【代码】

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;

int n,cnt;
int a[N+10];
vector<pair<int,int> > g[N+10];

void dfs(int x,long long now,int pre){
    if (now>a[x]) return;
    cnt++;
    int len = g[x].size();
    for (int i = 0;i < len;i++){
        int y = g[x][i].first;
        int w = g[x][i].second;
        if (y==pre) continue;
        dfs(y,max(1LL*0,now)+w,x);
    }
}

int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    cin >> n;
    for (int i = 1;i <= n;i++) cin >> a[i];
    for (int i = 2;i <= n;i++){
        int x = i,y,w;
        cin >> y >> w;
        g[x].push_back(make_pair(y,w));
        g[y].push_back(make_pair(x,w));
    }
    dfs(1,0,-1);
    cout<<n-cnt<<endl;
	return 0;
}