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;
const int mx=2e5+5;
vector<int>adj[mx];
int mark[mx];
int ans;
int dp[mx];
void dfs(int node,int par){
int sum=0;
int maxi=0;
for(auto it:adj[node]){
if(it!=par){
dfs(it,node);
sum+=dp[it];
maxi=max(maxi,dp[it]);
}
}
if(mark[node]){
ans=max(ans,maxi+1);
dp[node]=max(sum-1,1);
}else{
dp[node]=sum;
}
ans=max(ans,dp[node]);
}
int main(){
int n;cin>>n;
for(int i=0;i<n-1;i++){
int x,y;
cin>>x>>y;
x--;y--;
adj[x].push_back(y); adj[y].push_back(x);
}
string x;cin>>x;
for(int i=0;i<n;i++){
mark[i]=(x[i]=='1');
}
/*
for(int i=0;i<n;i++){
if(mark[i]){
dfs(i,i);
}
}*/
dfs(0,-1);
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |