#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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 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 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
2 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
4 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 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 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
2 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
4 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
6 ms |
5076 KB |
Output is correct |
23 |
Correct |
6 ms |
5076 KB |
Output is correct |
24 |
Correct |
6 ms |
5196 KB |
Output is correct |
25 |
Correct |
5 ms |
5076 KB |
Output is correct |
26 |
Correct |
5 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
5 ms |
5076 KB |
Output is correct |
29 |
Correct |
5 ms |
5204 KB |
Output is correct |
30 |
Correct |
5 ms |
5332 KB |
Output is correct |
31 |
Correct |
5 ms |
5196 KB |
Output is correct |
32 |
Correct |
7 ms |
5164 KB |
Output is correct |
33 |
Correct |
5 ms |
5076 KB |
Output is correct |
34 |
Correct |
6 ms |
5112 KB |
Output is correct |
35 |
Correct |
5 ms |
5076 KB |
Output is correct |
36 |
Correct |
5 ms |
5076 KB |
Output is correct |
37 |
Correct |
5 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
6 ms |
5204 KB |
Output is correct |
40 |
Correct |
5 ms |
5204 KB |
Output is correct |
41 |
Correct |
6 ms |
5204 KB |
Output is correct |
42 |
Correct |
5 ms |
5216 KB |
Output is correct |
43 |
Correct |
5 ms |
5204 KB |
Output is correct |
44 |
Correct |
4 ms |
5204 KB |
Output is correct |
45 |
Correct |
5 ms |
5204 KB |
Output is correct |
46 |
Correct |
5 ms |
5204 KB |
Output is correct |
47 |
Correct |
4 ms |
5204 KB |
Output is correct |
48 |
Correct |
4 ms |
5204 KB |
Output is correct |
49 |
Correct |
4 ms |
5204 KB |
Output is correct |
50 |
Correct |
4 ms |
5204 KB |
Output is correct |
51 |
Correct |
4 ms |
5204 KB |
Output is correct |
52 |
Correct |
6 ms |
5076 KB |
Output is correct |
53 |
Correct |
5 ms |
5204 KB |
Output is correct |
54 |
Correct |
5 ms |
5204 KB |
Output is correct |
55 |
Correct |
5 ms |
5204 KB |
Output is correct |
56 |
Correct |
4 ms |
5204 KB |
Output is correct |
57 |
Correct |
5 ms |
5204 KB |
Output is correct |
58 |
Correct |
4 ms |
5076 KB |
Output is correct |
59 |
Correct |
4 ms |
5076 KB |
Output is correct |
60 |
Correct |
5 ms |
5204 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
6 ms |
5204 KB |
Output is correct |
63 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 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 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
2 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
4 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
6 ms |
5076 KB |
Output is correct |
23 |
Correct |
6 ms |
5076 KB |
Output is correct |
24 |
Correct |
6 ms |
5196 KB |
Output is correct |
25 |
Correct |
5 ms |
5076 KB |
Output is correct |
26 |
Correct |
5 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
5 ms |
5076 KB |
Output is correct |
29 |
Correct |
5 ms |
5204 KB |
Output is correct |
30 |
Correct |
5 ms |
5332 KB |
Output is correct |
31 |
Correct |
5 ms |
5196 KB |
Output is correct |
32 |
Correct |
7 ms |
5164 KB |
Output is correct |
33 |
Correct |
5 ms |
5076 KB |
Output is correct |
34 |
Correct |
6 ms |
5112 KB |
Output is correct |
35 |
Correct |
5 ms |
5076 KB |
Output is correct |
36 |
Correct |
5 ms |
5076 KB |
Output is correct |
37 |
Correct |
5 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
6 ms |
5204 KB |
Output is correct |
40 |
Correct |
5 ms |
5204 KB |
Output is correct |
41 |
Correct |
6 ms |
5204 KB |
Output is correct |
42 |
Correct |
5 ms |
5216 KB |
Output is correct |
43 |
Correct |
5 ms |
5204 KB |
Output is correct |
44 |
Correct |
4 ms |
5204 KB |
Output is correct |
45 |
Correct |
5 ms |
5204 KB |
Output is correct |
46 |
Correct |
5 ms |
5204 KB |
Output is correct |
47 |
Correct |
4 ms |
5204 KB |
Output is correct |
48 |
Correct |
4 ms |
5204 KB |
Output is correct |
49 |
Correct |
4 ms |
5204 KB |
Output is correct |
50 |
Correct |
4 ms |
5204 KB |
Output is correct |
51 |
Correct |
4 ms |
5204 KB |
Output is correct |
52 |
Correct |
6 ms |
5076 KB |
Output is correct |
53 |
Correct |
5 ms |
5204 KB |
Output is correct |
54 |
Correct |
5 ms |
5204 KB |
Output is correct |
55 |
Correct |
5 ms |
5204 KB |
Output is correct |
56 |
Correct |
4 ms |
5204 KB |
Output is correct |
57 |
Correct |
5 ms |
5204 KB |
Output is correct |
58 |
Correct |
4 ms |
5076 KB |
Output is correct |
59 |
Correct |
4 ms |
5076 KB |
Output is correct |
60 |
Correct |
5 ms |
5204 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
6 ms |
5204 KB |
Output is correct |
63 |
Correct |
4 ms |
5076 KB |
Output is correct |
64 |
Correct |
623 ms |
11976 KB |
Output is correct |
65 |
Correct |
615 ms |
11996 KB |
Output is correct |
66 |
Correct |
564 ms |
11952 KB |
Output is correct |
67 |
Correct |
498 ms |
11992 KB |
Output is correct |
68 |
Correct |
496 ms |
11872 KB |
Output is correct |
69 |
Correct |
587 ms |
12056 KB |
Output is correct |
70 |
Correct |
553 ms |
11920 KB |
Output is correct |
71 |
Correct |
501 ms |
11976 KB |
Output is correct |
72 |
Correct |
538 ms |
12000 KB |
Output is correct |
73 |
Correct |
865 ms |
24564 KB |
Output is correct |
74 |
Correct |
885 ms |
24504 KB |
Output is correct |
75 |
Correct |
549 ms |
11908 KB |
Output is correct |
76 |
Correct |
498 ms |
11852 KB |
Output is correct |
77 |
Correct |
609 ms |
11868 KB |
Output is correct |
78 |
Correct |
625 ms |
11868 KB |
Output is correct |
79 |
Correct |
656 ms |
11960 KB |
Output is correct |
80 |
Correct |
529 ms |
11868 KB |
Output is correct |
81 |
Correct |
252 ms |
12356 KB |
Output is correct |
82 |
Correct |
246 ms |
14860 KB |
Output is correct |
83 |
Correct |
757 ms |
19812 KB |
Output is correct |
84 |
Correct |
811 ms |
21928 KB |
Output is correct |
85 |
Correct |
797 ms |
21552 KB |
Output is correct |
86 |
Correct |
662 ms |
16076 KB |
Output is correct |
87 |
Correct |
641 ms |
15524 KB |
Output is correct |
88 |
Correct |
485 ms |
15880 KB |
Output is correct |
89 |
Correct |
485 ms |
16044 KB |
Output is correct |
90 |
Correct |
483 ms |
15740 KB |
Output is correct |
91 |
Correct |
521 ms |
15940 KB |
Output is correct |
92 |
Correct |
451 ms |
16232 KB |
Output is correct |
93 |
Correct |
502 ms |
20312 KB |
Output is correct |
94 |
Correct |
532 ms |
21696 KB |
Output is correct |
95 |
Correct |
512 ms |
19092 KB |
Output is correct |
96 |
Correct |
496 ms |
17252 KB |
Output is correct |
97 |
Correct |
436 ms |
15708 KB |
Output is correct |
98 |
Correct |
427 ms |
15196 KB |
Output is correct |
99 |
Correct |
462 ms |
15116 KB |
Output is correct |
100 |
Correct |
462 ms |
18296 KB |
Output is correct |
101 |
Correct |
455 ms |
16252 KB |
Output is correct |
102 |
Correct |
491 ms |
15456 KB |
Output is correct |
103 |
Correct |
495 ms |
15260 KB |
Output is correct |
104 |
Correct |
207 ms |
15548 KB |
Output is correct |
105 |
Correct |
832 ms |
24124 KB |
Output is correct |
106 |
Correct |
160 ms |
15424 KB |
Output is correct |
107 |
Correct |
158 ms |
15336 KB |
Output is correct |
108 |
Correct |
838 ms |
28124 KB |
Output is correct |
109 |
Correct |
175 ms |
15600 KB |
Output is correct |
110 |
Correct |
870 ms |
28324 KB |
Output is correct |
111 |
Correct |
413 ms |
14544 KB |
Output is correct |