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;
typedef long long ll;
const int MX = 2e5 + 5;
int N;
vector<int> g[MX];
int dp[MX];
int ans = 0;
string s;
void dfs(int v, int p) {
int cur = 0, mx = 0; // cur = ambil >= 1, mx = ambil 1 yg maksimum
for(auto u : g[v]) {
if(u == p) continue;
dfs(u, v);
cur += max(0, dp[u]);
mx = max(mx, dp[u]);
}
if(s[v] == '1') {
dp[v] = max(1, cur - 1);
ans = max(ans, cur); // consider subtree ini aja
ans = max(ans, mx + 1);
} else {
dp[v] = cur;
ans = max(ans, cur);
}
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> N;
for(int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
cin >> s;
s = '#' + s;
dfs(1, 0);
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... |