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>
#define fi first
#define se second
#define rep(a, b) for(int a = 0; a < (b); a++)
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<ll> vl;
const int N=2e5+10;
int dp[N][2],t[N];
vi graf[N];
void dfs(int v,int o){
int xd=0,xd2=0,xd3=0;
for(auto u:graf[v]){
if(u==o) continue;
dfs(u,v);
xd=max(xd,dp[u][0]);
xd2=max(xd2,dp[u][1]);
xd3+=dp[u][1];
}
if(t[v]) dp[v][0]=max({xd3-1,xd,xd2+1,1}),dp[v][1]=max({1,xd3-1});
else dp[v][0]=max({xd3,xd,0}),dp[v][1]=xd3;
}
void solve(){
int n;
cin>>n;
rep(i,n-1){
int a,b;
cin>>a>>b;
graf[a].push_back(b),graf[b].push_back(a);
}
rep(i,n){
char a;
cin>>a;
t[i+1]=int(a)-48;
}
dfs(1,1);
cout<<dp[1][0]<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |