题解 CF682C 【Alyona and the Tree】
生活随笔
收集整理的這篇文章主要介紹了
题解 CF682C 【Alyona and the Tree】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡單搜索題,我們每找到一組不滿足題目給出條件的點和邊就將其整個子樹刪除,然后最終答案加上該子樹的大小即可。注意,搜索的時候如果當前的邊權和sum已經為負了,應該將其改為0(可以想想為什么)
注:題目翻譯有誤
原文中的小于應該改為小于等于
#include<bits/stdc++.h> #define maxn 100005 using namespace std; inline char get(){static char buf[30000],*p1=buf,*p2=buf;return p1==p2 && (p2=(p1=buf)+fread(buf,1,30000,stdin),p1==p2)?EOF:*p1++; } inline int read(){register char c=get();register int f=1,_=0;while(c>'9' || c<'0')f=(c=='-')?-1:1,c=get();while(c<='9' && c>='0')_=(_<<3)+(_<<1)+(c^48),c=get();return _*f; } struct edge{int u,v,w,next; }E[maxn<<1]; int p[maxn],eid; inline void init(){for(register int i=0;i<maxn;i++)p[i]=-1;eid=0; } inline void insert(int u,int v,int w){E[eid].u=u;E[eid].v=v;E[eid].w=w;E[eid].next=p[u];p[u]=eid++; } inline void insert2(int u,int v,int w){insert(u,v,w);insert(v,u,w); } int n; int a[maxn]; int fa[maxn],size[maxn]; int ans=0; inline void get_size(int u,int fa){size[u]=1;for(register int i=p[u];~i;i=E[i].next){int v=E[i].v;if(v==fa)continue;get_size(v,u);size[u]+=size[v];} } inline void dfs(int u,int fa,int sum){if(sum>a[u]){ans+=size[u];//cout<<sum<<" "<<a[u]<<endl;return;}for(register int i=p[u];~i;i=E[i].next){int v=E[i].v;int w=E[i].w;if(v==fa)continue;dfs(v,u,max(sum+w,0));} } int main(){//freopen("1.txt","r",stdin);init();n=read();for(register int i=1;i<=n;i++)a[i]=read();fa[1]=-1;for(register int i=2;i<=n;i++){int p=read(),c=read();fa[i]=p;insert2(i,p,c);}get_size(1,-1);dfs(1,-1,0);cout<<ans<<endl;return 0; }轉載于:https://www.cnblogs.com/Chen574118090/p/10460055.html
總結
以上是生活随笔為你收集整理的题解 CF682C 【Alyona and the Tree】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么会梦到被追杀
- 下一篇: 力扣——k个一组翻转链表