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;
#define pb push_back
const int MAX_N = 2e5;
int dp[1 + MAX_N];
vector <int> gr[1 + MAX_N];
int dfs_calc (int node, int par, string &gen) {
int mx = 0;
int is_gen = gen[node] - '0';
int ans = 0;
for (int son : gr[node])
if (son != par) {
ans = max (ans, dfs_calc (son, node, gen));
dp[node] += dp[son];
mx = max (mx, dp[son]);
}
dp[node] = max (is_gen, dp[node] - is_gen);
ans = max ({ans, mx + is_gen, dp[node]});
return ans;
}
int main() {
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
gr[x].pb (y);
gr[y].pb (x);
}
string generators;
cin >> generators; generators = " " + generators;
cout << dfs_calc (1, 0, generators) << "\n";
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... |