제출 #781907

#제출 시각아이디문제언어결과실행 시간메모리
7819071binPower Plant (JOI20_power)C++14
100 / 100
126 ms28888 KiB
#include <bits/stdc++.h>

using namespace std;

#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 2e5 + 5;
int n, ans, a[NMAX], u, v;
vector<int> adj[NMAX];
string s;

int dfs(int x, int p){
    int v1 = 0, v2 = 0;
    for(int& nx : adj[x]){
        if(nx == p) continue;
        int t = dfs(nx, x);
        v1 = max(v1, t);
        v2 += max(t, 0);
    }
    ans = max(ans, v1 + a[x]);
    ans = max(ans, v2 - a[x]);
    return max(v2 - a[x], a[x]);
}

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n;
    for(int i = 1; i < n; i++){
        cin >> u >> v;
        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
    }
    cin >>s;
    for(int i = 1; i <= n; i++) a[i] = s[i - 1] - '0';
    dfs(1, -1);
    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...