Submission #1007353

#TimeUsernameProblemLanguageResultExecution timeMemory
1007353Alihan_8Power Plant (JOI20_power)C++17
100 / 100
69 ms26260 KiB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long

using i64 = long long;

template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}

template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}

const int inf = 1e9;

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n; cin >> n;

    vector <vector<int>> adj(n);

    for ( int i = 1; i < n; i++ ){
        int u, v; cin >> u >> v;

        --u, --v;

        adj[u].pb(v);
        adj[v].pb(u);
    }

    string s; cin >> s;

    int cnt = 0;

    for ( auto &u: s ){
        u -= '0';

        cnt += 1;
    }

    vector <int> dp(n);

    int ans = 0;

    auto dfs = [&](auto dfs, int u, int p) -> void{
        int cnt = 0, ma = 0;

        for ( auto &v: adj[u] ){
            if ( v != p ){
                dfs(dfs, v, u);

                cnt += dp[v];

                chmax(ma, dp[v]);
            }
        }

        if ( s[u] > 0 ){
            chmax(ans, max(cnt - 1, ma + 1));
        } else{
            chmax(ans, cnt);
        }

        dp[u] = max((int)s[u], cnt - s[u]);
    };

    dfs(dfs, 0, -1);

    cout << ans << ln;

    cout << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...