#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;
for(auto&x:con[CN]){
if(existe[x])continue;
tot+=explora(x,CN);
}
if(valores[CN]=='1'){
--tot;
tot=std::max(tot,1);
}
ans=std::max(ans,tot);
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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5224 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Incorrect |
3 ms |
5124 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5224 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Incorrect |
3 ms |
5124 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5224 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Incorrect |
3 ms |
5124 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |