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;
using ll = long long;
const int N = 2e5 + 10;
vector<int> g[N];
int dp[N];
int n;
string s;
int res;
void dfs(int v, int p) {
int sum = 0, mx = 0;
for(int to : g[v]) {
if(to == p) continue;
dfs(to, v);
mx = max(mx, dp[to]);
sum += max(dp[to], 0);
}
dp[v] = max(s[v - 1] - '0', sum - (s[v - 1] - '0'));
res = max(res, dp[v]);
if(s[v - 1] == '1')
res = max(res, mx + 1);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
cin >> s;
dfs(1, 1);
// for(int i = 1; i <= n; i++) {
// cout << dp[i] << ' ';
// }
// cout << '\n';
cout << res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |