This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 5;
int ans;
vector <int> graph[MAX_N];
int power[MAX_N];
int dp[MAX_N];
void dfs(int u, int p) {
int ma = 0;
for(auto v : graph[u]) {
if(v == p) {
continue;
}
dfs(v, u);
ma = max(ma, dp[v]);
dp[u] += dp[v];
}
dp[u] = max(dp[u] - power[u], power[u]);
ans = max({ans, dp[u], ma + power[u]});
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N;
cin >> N;
for(int i = 1; i <= N - 1; i++) {
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
for(int i = 1; i <= N; i++) {
char c;
cin >> c;
power[i] = c - '0';
}
dfs(1, -1);
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |