Submission #534694

#TimeUsernameProblemLanguageResultExecution timeMemory
534694michaoPower Plant (JOI20_power)C++14
47 / 100
1502 ms45380 KiB
#include <bits/stdc++.h> #define int long long #define mp make_pair #define pb push_back #define ld long double #define pii pair<int,int> #define sz(x) (int)x.size() #define piii pair<pii,pii> #define precise cout<<fixed<<setprecision(10) #define st first #define nd second #define ins insert #define vi vector<int> #define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) using namespace std; const int MAX=2e5+5; vi P[MAX]; int c[MAX]; int dp[MAX]; char s[MAX]; int ans=0; void dfs(int u,int fa){ vi d; d.clear(); vi wrzut; for (auto it:P[u]){ if (it==fa)continue; dfs(it,u); d.pb(it); wrzut.pb(dp[it]); } if (c[u]==0){ for (auto v:d)dp[u]+=dp[v]; ans=max(ans,dp[u]); } else{ assert(c[u]==1); int sum=0; for (auto v:d)sum+=dp[v]; if (sum==0)dp[u]=1,ans=max(ans,dp[u]); else{ dp[u]=max(1LL,sum-1); sort(wrzut.begin(),wrzut.end()); if (sz(wrzut)){ ans=max(ans,wrzut.back()+1); } ans=max(ans,dp[u]); } } } int32_t main(){ BOOST; int n; cin>>n; for (int i=1;i<=n-1;i++){ int a,b; cin>>a>>b; P[a].pb(b); P[b].pb(a); } for (int i=1;i<=n;i++){ cin>>s[i]; c[i]=s[i]-'0'; } dfs(1,-1); for (int i=1;i<=n;i++)cerr<<"DPEK "<<i<<" "<<dp[i]<<"\n"; cout<<ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...