제출 #347391

#제출 시각아이디문제언어결과실행 시간메모리
347391dolphingarlicPower Plant (JOI20_power)C++14
0 / 100
4 ms5248 KiB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

string s;
vector<int> graph[200001];
int dp[200001], ans = 1;

void dfs(int node = 1, int parent = 0) {
    int cnt = 0;
    for (int i : graph[node]) if (i != parent) {
        dfs(i, node);
        dp[node] += dp[i];
        if (dp[i]) cnt++;
    }
    ans = max(ans, dp[node] + (s[node - 1] == '1' && cnt < 2));
    dp[node] = max(s[node - 1] - '0', dp[node] - s[node - 1] + '0');
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n;
    cin >> n;
    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    cin >> s;
    dfs();
    cout << ans << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...