#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define szz(r) (ll)r.size()
#define vi vector<int>
#define vvi vector<vi>
#define pp pair<ll,int>
#define ub(x,i) upper_bound(all(x),i)-x.begin()
using namespace std;
const int mxn=2e5+5;
vector<int>g[mxn];
string s;
int rs=0;
int dp[mxn]{0};
void dfs(int u,int p){
int mx=0;
for(auto v:g[u]){
if(v==p)continue;
dfs(v,u);
dp[u]+=dp[v];
mx=max(mx,dp[v]);
}
if(s[u]=='1')dp[u]=max(dp[u]-1,1),rs=max({rs,dp[u],mx+1});
if(s[u]=='0')dp[u]=max(dp[u],0),rs=max({rs,dp[u],mx});
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n;cin>>n;
for(int i=1;i<=n-1;i++){
int a,b;cin>>a>>b;g[a].pb(b);g[b].pb(a);
}cin>>s;s="1"+s;
dfs(1,1);cout<<rs;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |