#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
vector<int> adj[N];
int dp[N][2];
string s;
void dfs(int u,int p=-1){
int mx=0;
dp[u][0]=0;
dp[u][1]=s[u]-'0';
for(int v:adj[u]){
if(v==p) continue;
dfs(v,u);
dp[u][0]+=max({dp[v][1]-(s[v]-'0')*2,dp[v][0]});
dp[u][1]=max({dp[u][1],dp[v][0]+s[u]-'0',dp[v][1]});
}
dp[u][0]=max(s[u]-'0',dp[u][0]-(s[u]-'0'));
if(mx>0) dp[u][1]+=mx;
}
int main(){
int n;
cin>>n;
for(int i=0;i<n-1;i++){
int a,b;
cin>>a >>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
cin>>s;
s=" "+s;
int root;
for(int i=1;i<=n;i++){
if(s[i]=='1'){
dfs(i);
root=i;
break;
}
}
cout<<max(dp[root][0],dp[root][1]);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |