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;
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
vector<vector<int>> g(n + 1);
for(int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
g[a].emplace_back(b);
g[b].emplace_back(a);
}
string s;
cin >> s;
s = "#" + s;
vector<int> dp(n + 1);
int ans = 0;
function<void(int, int)> dfs = [&](int u, int p) {
int sum = 0;
for(int to : g[u]) {
if(to == p) {
continue;
}
dfs(to, u);
sum += dp[to];
if(s[u] == '1') {
ans = max(ans, dp[to] + 1);
}
}
dp[u] = sum;
if(s[u] == '1') {
dp[u]--;
dp[u] = max(dp[u], 1);
}
ans = max(ans, dp[u]);
};
dfs(1, 1);
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |