Submission #623027

#TimeUsernameProblemLanguageResultExecution timeMemory
623027S2speedPower Plant (JOI20_power)C++17
100 / 100
160 ms29832 KiB
#include<bits/stdc++.h> using namespace std; #pragma GCC optimize ("Ofast") typedef long long ll; const ll maxn = 2e5 + 17; vector<ll> adj[maxn]; string s; ll dp[maxn] , ans = 0; void DFS(ll r , ll par){ ll h = 0 , m = -1; for(auto i : adj[r]){ if(i == par) continue; DFS(i , r); h += dp[i]; m = max(m , dp[i]); } ans = max(ans , m + (s[r] == '1')); if(h == 0){ dp[r] = (s[r] == '1'); } else { dp[r] = max((ll)(s[r] == '1') , h - (s[r] == '1')); } ans = max(ans , dp[r]); return; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n; cin>>n; for(ll i = 1 ; i < n ; i++){ ll v , u; cin>>v>>u; v--; u--; adj[v].push_back(u); adj[u].push_back(v); } cin>>s; DFS(0 , -1); cout<<ans<<'\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...