Submission #984650

#TimeUsernameProblemLanguageResultExecution timeMemory
984650aaaaaarrozPower Plant (JOI20_power)C++17
100 / 100
196 ms32696 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, x, y, answer = 0;
string power;
vector<ll> adj[200005];
ll dp[200005], ans[200005];
vector<ll> visited;
    
void dfs(ll from)
{
    visited[from] = true;
    ll summ = 0, maxx = 0;
    for (ll u: adj[from])
    {
        if (visited[u]) continue;
        dfs(u);
        summ+=dp[u];
        maxx = max(maxx, dp[u]);
    }
    if (power[from-1]=='1') {
        ans[from] = max(summ-1, maxx + 1); 
        dp[from] = max(1LL, summ-1);
    }
    else {
        ans[from] = dp[from] = summ;
    }
    answer = max(answer, ans[from]);
}
int main()
{
    cin>>n;
    for (ll i=0; i<n-1; ++i)
    {
        cin>>x>>y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    cin >> power;
    visited.assign(n+1, false);
    dfs(1);
    cout<<answer<<"\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...