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 MAX 205000
std::vector<int> con[MAX];
bool existe[MAX];
int dfs(int pos,int prev){
int size=1;
for(auto&x:con[pos]){
if(x==prev||existe[x])continue;
size+=dfs(x,pos);
}
return size;
}
int centroide;
int find_centroide(int pos,int prev,int sz){
int size=1;
int max=0;
for(auto&x:con[pos]){
if(x==prev||existe[x])continue;
int b =find_centroide(x,pos,sz);
size+=b;
max=std::max(max,b);
}
max=std::max(max,sz-size);
if(max<=sz/2)centroide=pos;
return size;
}
std::string valores;
int explora(int pos,int prev){
int ans=0;
if(valores[pos]=='1')ans=1;
int soma=0;
for(auto&x:con[pos]){
if(x==prev||existe[x])continue;
soma+=explora(x,pos);
}
if(valores[pos]=='1')--soma;
return std::max(ans,soma);
}
int N;
int ans=0;
void Decompor(int Y){
int SZ = dfs(Y,-1);
find_centroide(Y,-1,SZ);
int CN = centroide;
/// std::cout<<"Centro: "<<CN<<" "<<Y<<" "<<SZ<<"\n";
existe[CN]=true;
int tot=0;
int ans2=0;
for(auto&x:con[CN]){
if(existe[x])continue;
int b=explora(x,CN);
tot+=b;
if(valores[CN]=='1')ans2=std::max(ans2,b+1);
}
if(valores[CN]=='1'){
--tot;
tot=std::max(tot,1);
}
ans=std::max(ans,tot);
ans=std::max(ans,ans2);
for(auto&x:con[CN]){
if(existe[x])continue;
Decompor(x);
}
}
int main()
{
std::cin>>N;
for(int i=1;i!=N;++i){
int a,b;
std::cin>>a>>b;
--a;--b;
con[a].push_back(b);
con[b].push_back(a);
}
std::cin>>valores;
Decompor(0);
std::cout<<ans<<"\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... |