Submission #1113931

#TimeUsernameProblemLanguageResultExecution timeMemory
1113931Dan4LifePower Plant (JOI20_power)C++17
100 / 100
127 ms34500 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define sz(a) (int)a.size() #define all(a) begin(a),end(a) const int N = (int)2e5+2; int n; string gen; vector<int> adj[N]; int dp[N], ans; int dfs(int s, int p){ vector<int> v; v.clear(); for(auto u : adj[s]){ if(u==p) continue; dfs(u,s); v.pb(dp[u]); } int sum = 0, best = 0, ok = (gen[s]=='1'); for(auto u : v) sum+=u, best=max(best,u); ans = max({ans, sum-ok, best+ok}); dp[s] = max(ok,sum-ok); return ans; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for(int i = 1; i < n; i++){ int a, b; cin >> a >> b; adj[a].pb(b), adj[b].pb(a); } cin >> gen; gen='$'+gen; cout << dfs(1,-1) << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...