【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;
}
相关文章
- 创龙OMAPL138开发板测试Device is held in reset. Take the device out of reset, and retry the operation.
- Database 'xxx' cannot be upgraded because it is read-only or has read-only file Make the database or files writeable, and rerun recovery.
- [Typescript] Creating Chainable Method Abstractions with Generics and the Builder Pattern - 05
- [Angular 9] Improved Dependency Injection with the new providedIn scopes 'any' and 'platform'
- [TypeScript] Installing TypeScript and Running the TypeScript Compiler (tsc)
- The magic method __set() must have public visibility and cannot be static in
- is not in the sudoers file 解决(转)
- [Functional Programming] Discard the High and Low Values From an Array of Numbers with Ramda
- [Angular 9] Improved Dependency Injection with the new providedIn scopes 'any' and 'platform'
- [HTML5] Show Images of Differing Resolutions Depending on the Viewport Width with srcset
- [Angular2 Router] Optional Route Query Parameters - The queryParams Directive and the Query Parameters Observable
- how to replace the note reuse project into my own one
- Unable to read the project file 'client.csproj'. Could not load file or assembly 'Microsoft.Build.En
- 【Codeforces 1083A】The Fair Nut and the Best Path
- 【52.49%】【codeforces 556A】Case of the Zeros and Ones
- 【Codeforces Round #435 (Div. 2) B】Mahmoud and Ehab and the bipartiteness
- 【Codeforces Round #430 (Div. 2) C】Ilya And The Tree
- 【Codeforces Round #426 (Div. 2) C】The Meaningless Game
- linux 空间不够了,怎么办?Disk Requirements:At least 11MB more space needed on the / filesystem.
- 成功解决Not possible to connect to the Web3 provider. Make sure the provider is running and a connection
- Codeforces 437E The Child and Polygon(间隔DP)
- Codeforces #250 (Div. 2) C.The Child and Toy
- 使用@Qualifier注解时候,报错:The annotation @Qualifier is disallowed for this location