Submission #1140502

#TimeUsernameProblemLanguageResultExecution timeMemory
1140502browntoadPower Plant (JOI20_power)C++20
100 / 100
79 ms31164 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n+1)
#define pii pair<int, int>
#define f first
#define s second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)((x).size())
#define pb push_back
#define endl '\n'
#define IOS() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

const ll maxn = 2e5+5;
const ll inf = 1ll<<60;
const ll mod = 1e9+7;

int n;

int ans = 1;
vector<int> dp(maxn);
vector<int> g[maxn];
vector<int> c(maxn);

void dfs(int x, int pre){
    int tp = (x > 1 && c[pre]);
    for (auto y:g[x]){
        if (y == pre) continue;
        dfs(y, x);
        dp[x] += max((int)(c[y]), dp[y]);
    }
    dp[x] -= c[x];

    ans = max(ans, dp[x]+tp);
}
signed main(){
    IOS();
    cin>>n;
    REP(i, n-1){
        int u, v; cin>>u>>v;
        g[u].pb(v);
        g[v].pb(u);
    }
    string str; cin>>str;
    str = '$'+str;
    REP1(i, n) c[i] = (str[i] == '1');

    REP1(i, n){
        for (auto y:g[i]) ans = max(ans, c[i]+c[y]);
    }

    dfs(1, -1);
    cout<<ans<<endl;


}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...