제출 #1019872

#제출 시각아이디문제언어결과실행 시간메모리
1019872AndreyPower Plant (JOI20_power)C++14
100 / 100
87 ms29268 KiB
#include<bits/stdc++.h>
using namespace std;

vector<int> haha[200001];
vector<int> dp(200001);
vector<int> col(200001);
int ans = 0;

void dfs(int a, int t) {
    int sb = 0;
    for(int v: haha[a]) {
        if(v != t) {
            dfs(v,a);
            sb+=dp[v];
        }
    }
    sb-=col[a];
    dp[a] = max(col[a],sb);
}

void dude(int a, int t, int u) {
    int big = u,sb = u-col[a];
    for(int v: haha[a]) {
        if(v != t) {
            big = max(big,dp[v]);
            sb+=dp[v];
        }
    }
    if(col[a]) {
        ans = max(ans,big+1);
    }
    for(int v: haha[a]) {
        if(v != t) {
            dude(v,a,max(col[a],sb-dp[v]));
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
    cout.tie(NULL);
    int n,a,b;
    cin >> n;
    for(int i = 0; i < n-1; i++) {
        cin >> a >> b;
        haha[a].push_back(b);
        haha[b].push_back(a);
    }
    for(int i = 1; i <= n; i++) {
        char a;
        cin >> a;
        if(a == '1') {
            col[i] = 1;
        }
    }
    dfs(1,-1);
    dude(1,-1,0);
    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...