#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<int> g[n + 1];
for (int i = 1; i < n; i++){
int x, y; cin>>x>>y;
g[x].pb(y);
g[y].pb(x);
}
vector<bool> a(n + 1);
for (int i = 1; i <= n; i++){
char x; cin>>x;
a[i] = (x == '1');
}
int dp[n + 1][2];
function<void(int, int)> dfs = [&](int v, int pr){
dp[v][0] = 0; dp[v][1] = a[v];
for (int i: g[v]){
if (i == pr) continue;
dfs(i, v);
dp[v][0] += max({0, dp[i][1] - 2, dp[i][0]});
dp[v][1] = max({dp[v][1], dp[i][0] + 1, dp[i][1] - 1});
}
dp[v][0] = max((int) a[v], dp[v][0] - a[v]);
if (!a[v]) dp[v][1] = 0;
};
dfs(1, 0);
int out = 0;
for (int i = 1; i <= n; i++){
out = max({out, dp[i][0], dp[i][1]});
}
cout<<out<<"\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... |