#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5;
const int INF = 1e9;
bool hasPP[MAX_N + 1], vis[MAX_N + 1];
int sizee[MAX_N + 1], dp[MAX_N + 1];
vector<int> edges[MAX_N + 1];
int centroid;
void findCentroid( int u, int p, int sizeTotal ) {
int maxSize = 0;
for ( int v: edges[u] ) {
if ( v == p || vis[v] )
continue;
findCentroid( v, u , sizeTotal);
maxSize = max( maxSize, sizee[v] );
}
maxSize = max( maxSize, sizeTotal - sizee[u] );
if ( maxSize <= sizeTotal / 2 )
centroid = u;
}
void calcSizes( int u, int p ) {
sizee[u] = 1;
for ( int v: edges[u] ) {
if ( v == p || vis[v] )
continue;
calcSizes( v, u );
sizee[u] += sizee[v];
}
}
void calcDP( int u, int p ) {
int sum = 0;
for ( int v: edges[u] ) {
if ( v == p || vis[v] )
continue;
calcDP( v, u );
sum += (dp[v] >= 0 ? dp[v] : 0);
}
dp[u] = max( sum > 0 ? sum - hasPP[u] : -INF, (hasPP[u] ? 1 : -INF) );
}
int maxProfit = 1;
void decomp( int u ) {
calcSizes( u, 0 );
centroid = -1;
findCentroid( u, 0, sizee[u] );
int c = centroid;
vis[c] = true;
int maxDP[3];
maxDP[0] = 0;
maxDP[1] = maxDP[2] = -INF;
for ( int v: edges[c] ) {
if ( vis[v] )
continue;
calcDP( v, c );
maxDP[2] = max( maxDP[2], max( maxDP[1], maxDP[2] ) + dp[v] );
maxDP[1] = max( maxDP[1], maxDP[0] + dp[v] );
}
if ( hasPP[c] )
maxProfit = max( maxProfit, maxDP[1] + 1 );
maxProfit = max( maxProfit, maxDP[2] - hasPP[c] );
for ( int v: edges[c] ) {
if ( vis[v] )
continue;
decomp( v );
}
}
int main() {
int n;
cin >> n;
for ( int i = 0; i < n - 1; i++ ) {
int u, v;
cin >> u >> v;
edges[u].push_back( v );
edges[v].push_back( u );
}
for ( int u = 1; u <= n; u++ ) {
char ch;
cin >> ch;
hasPP[u] = ch - '0';
}
decomp( 1 );
cout << maxProfit;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6744 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6744 KB |
Output is correct |
6 |
Correct |
1 ms |
6744 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6744 KB |
Output is correct |
9 |
Correct |
1 ms |
6744 KB |
Output is correct |
10 |
Correct |
3 ms |
6744 KB |
Output is correct |
11 |
Correct |
1 ms |
6744 KB |
Output is correct |
12 |
Correct |
1 ms |
6744 KB |
Output is correct |
13 |
Correct |
1 ms |
6744 KB |
Output is correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6744 KB |
Output is correct |
16 |
Correct |
2 ms |
6744 KB |
Output is correct |
17 |
Correct |
1 ms |
6744 KB |
Output is correct |
18 |
Correct |
1 ms |
6744 KB |
Output is correct |
19 |
Correct |
1 ms |
6744 KB |
Output is correct |
20 |
Correct |
1 ms |
6744 KB |
Output is correct |
21 |
Correct |
1 ms |
6744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6744 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6744 KB |
Output is correct |
6 |
Correct |
1 ms |
6744 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6744 KB |
Output is correct |
9 |
Correct |
1 ms |
6744 KB |
Output is correct |
10 |
Correct |
3 ms |
6744 KB |
Output is correct |
11 |
Correct |
1 ms |
6744 KB |
Output is correct |
12 |
Correct |
1 ms |
6744 KB |
Output is correct |
13 |
Correct |
1 ms |
6744 KB |
Output is correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6744 KB |
Output is correct |
16 |
Correct |
2 ms |
6744 KB |
Output is correct |
17 |
Correct |
1 ms |
6744 KB |
Output is correct |
18 |
Correct |
1 ms |
6744 KB |
Output is correct |
19 |
Correct |
1 ms |
6744 KB |
Output is correct |
20 |
Correct |
1 ms |
6744 KB |
Output is correct |
21 |
Correct |
1 ms |
6744 KB |
Output is correct |
22 |
Correct |
3 ms |
6744 KB |
Output is correct |
23 |
Correct |
3 ms |
6748 KB |
Output is correct |
24 |
Correct |
3 ms |
6748 KB |
Output is correct |
25 |
Correct |
3 ms |
7000 KB |
Output is correct |
26 |
Correct |
4 ms |
6744 KB |
Output is correct |
27 |
Correct |
3 ms |
6744 KB |
Output is correct |
28 |
Correct |
3 ms |
6896 KB |
Output is correct |
29 |
Correct |
3 ms |
6748 KB |
Output is correct |
30 |
Correct |
3 ms |
6756 KB |
Output is correct |
31 |
Correct |
3 ms |
6744 KB |
Output is correct |
32 |
Correct |
3 ms |
6744 KB |
Output is correct |
33 |
Correct |
3 ms |
6748 KB |
Output is correct |
34 |
Correct |
3 ms |
6744 KB |
Output is correct |
35 |
Correct |
3 ms |
6744 KB |
Output is correct |
36 |
Correct |
3 ms |
6744 KB |
Output is correct |
37 |
Correct |
3 ms |
6748 KB |
Output is correct |
38 |
Correct |
2 ms |
6744 KB |
Output is correct |
39 |
Correct |
3 ms |
6744 KB |
Output is correct |
40 |
Correct |
4 ms |
7000 KB |
Output is correct |
41 |
Correct |
3 ms |
6744 KB |
Output is correct |
42 |
Correct |
3 ms |
6744 KB |
Output is correct |
43 |
Correct |
3 ms |
6744 KB |
Output is correct |
44 |
Correct |
3 ms |
6744 KB |
Output is correct |
45 |
Correct |
3 ms |
6744 KB |
Output is correct |
46 |
Correct |
3 ms |
6748 KB |
Output is correct |
47 |
Correct |
3 ms |
6744 KB |
Output is correct |
48 |
Correct |
3 ms |
6744 KB |
Output is correct |
49 |
Correct |
3 ms |
6744 KB |
Output is correct |
50 |
Correct |
3 ms |
6744 KB |
Output is correct |
51 |
Correct |
3 ms |
6744 KB |
Output is correct |
52 |
Correct |
3 ms |
6748 KB |
Output is correct |
53 |
Correct |
3 ms |
6744 KB |
Output is correct |
54 |
Correct |
3 ms |
6744 KB |
Output is correct |
55 |
Correct |
3 ms |
6744 KB |
Output is correct |
56 |
Correct |
2 ms |
6744 KB |
Output is correct |
57 |
Correct |
3 ms |
7004 KB |
Output is correct |
58 |
Correct |
2 ms |
6744 KB |
Output is correct |
59 |
Correct |
2 ms |
6744 KB |
Output is correct |
60 |
Correct |
3 ms |
6744 KB |
Output is correct |
61 |
Correct |
2 ms |
6744 KB |
Output is correct |
62 |
Correct |
3 ms |
6744 KB |
Output is correct |
63 |
Correct |
2 ms |
6744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6744 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6744 KB |
Output is correct |
6 |
Correct |
1 ms |
6744 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6744 KB |
Output is correct |
9 |
Correct |
1 ms |
6744 KB |
Output is correct |
10 |
Correct |
3 ms |
6744 KB |
Output is correct |
11 |
Correct |
1 ms |
6744 KB |
Output is correct |
12 |
Correct |
1 ms |
6744 KB |
Output is correct |
13 |
Correct |
1 ms |
6744 KB |
Output is correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6744 KB |
Output is correct |
16 |
Correct |
2 ms |
6744 KB |
Output is correct |
17 |
Correct |
1 ms |
6744 KB |
Output is correct |
18 |
Correct |
1 ms |
6744 KB |
Output is correct |
19 |
Correct |
1 ms |
6744 KB |
Output is correct |
20 |
Correct |
1 ms |
6744 KB |
Output is correct |
21 |
Correct |
1 ms |
6744 KB |
Output is correct |
22 |
Correct |
3 ms |
6744 KB |
Output is correct |
23 |
Correct |
3 ms |
6748 KB |
Output is correct |
24 |
Correct |
3 ms |
6748 KB |
Output is correct |
25 |
Correct |
3 ms |
7000 KB |
Output is correct |
26 |
Correct |
4 ms |
6744 KB |
Output is correct |
27 |
Correct |
3 ms |
6744 KB |
Output is correct |
28 |
Correct |
3 ms |
6896 KB |
Output is correct |
29 |
Correct |
3 ms |
6748 KB |
Output is correct |
30 |
Correct |
3 ms |
6756 KB |
Output is correct |
31 |
Correct |
3 ms |
6744 KB |
Output is correct |
32 |
Correct |
3 ms |
6744 KB |
Output is correct |
33 |
Correct |
3 ms |
6748 KB |
Output is correct |
34 |
Correct |
3 ms |
6744 KB |
Output is correct |
35 |
Correct |
3 ms |
6744 KB |
Output is correct |
36 |
Correct |
3 ms |
6744 KB |
Output is correct |
37 |
Correct |
3 ms |
6748 KB |
Output is correct |
38 |
Correct |
2 ms |
6744 KB |
Output is correct |
39 |
Correct |
3 ms |
6744 KB |
Output is correct |
40 |
Correct |
4 ms |
7000 KB |
Output is correct |
41 |
Correct |
3 ms |
6744 KB |
Output is correct |
42 |
Correct |
3 ms |
6744 KB |
Output is correct |
43 |
Correct |
3 ms |
6744 KB |
Output is correct |
44 |
Correct |
3 ms |
6744 KB |
Output is correct |
45 |
Correct |
3 ms |
6744 KB |
Output is correct |
46 |
Correct |
3 ms |
6748 KB |
Output is correct |
47 |
Correct |
3 ms |
6744 KB |
Output is correct |
48 |
Correct |
3 ms |
6744 KB |
Output is correct |
49 |
Correct |
3 ms |
6744 KB |
Output is correct |
50 |
Correct |
3 ms |
6744 KB |
Output is correct |
51 |
Correct |
3 ms |
6744 KB |
Output is correct |
52 |
Correct |
3 ms |
6748 KB |
Output is correct |
53 |
Correct |
3 ms |
6744 KB |
Output is correct |
54 |
Correct |
3 ms |
6744 KB |
Output is correct |
55 |
Correct |
3 ms |
6744 KB |
Output is correct |
56 |
Correct |
2 ms |
6744 KB |
Output is correct |
57 |
Correct |
3 ms |
7004 KB |
Output is correct |
58 |
Correct |
2 ms |
6744 KB |
Output is correct |
59 |
Correct |
2 ms |
6744 KB |
Output is correct |
60 |
Correct |
3 ms |
6744 KB |
Output is correct |
61 |
Correct |
2 ms |
6744 KB |
Output is correct |
62 |
Correct |
3 ms |
6744 KB |
Output is correct |
63 |
Correct |
2 ms |
6744 KB |
Output is correct |
64 |
Correct |
344 ms |
16160 KB |
Output is correct |
65 |
Correct |
323 ms |
15956 KB |
Output is correct |
66 |
Correct |
331 ms |
15956 KB |
Output is correct |
67 |
Correct |
309 ms |
15956 KB |
Output is correct |
68 |
Correct |
327 ms |
15952 KB |
Output is correct |
69 |
Correct |
319 ms |
16292 KB |
Output is correct |
70 |
Correct |
354 ms |
15952 KB |
Output is correct |
71 |
Correct |
338 ms |
16156 KB |
Output is correct |
72 |
Correct |
340 ms |
16164 KB |
Output is correct |
73 |
Correct |
584 ms |
31308 KB |
Output is correct |
74 |
Correct |
607 ms |
31236 KB |
Output is correct |
75 |
Correct |
286 ms |
15912 KB |
Output is correct |
76 |
Correct |
332 ms |
15956 KB |
Output is correct |
77 |
Correct |
384 ms |
16040 KB |
Output is correct |
78 |
Correct |
363 ms |
15864 KB |
Output is correct |
79 |
Correct |
335 ms |
15952 KB |
Output is correct |
80 |
Correct |
326 ms |
15860 KB |
Output is correct |
81 |
Correct |
168 ms |
16328 KB |
Output is correct |
82 |
Correct |
148 ms |
16080 KB |
Output is correct |
83 |
Correct |
436 ms |
22388 KB |
Output is correct |
84 |
Correct |
496 ms |
24968 KB |
Output is correct |
85 |
Correct |
452 ms |
24296 KB |
Output is correct |
86 |
Correct |
417 ms |
17772 KB |
Output is correct |
87 |
Correct |
406 ms |
16976 KB |
Output is correct |
88 |
Correct |
252 ms |
17512 KB |
Output is correct |
89 |
Correct |
279 ms |
17532 KB |
Output is correct |
90 |
Correct |
249 ms |
17272 KB |
Output is correct |
91 |
Correct |
304 ms |
17548 KB |
Output is correct |
92 |
Correct |
278 ms |
17948 KB |
Output is correct |
93 |
Correct |
295 ms |
22828 KB |
Output is correct |
94 |
Correct |
332 ms |
24376 KB |
Output is correct |
95 |
Correct |
283 ms |
21192 KB |
Output is correct |
96 |
Correct |
315 ms |
19024 KB |
Output is correct |
97 |
Correct |
239 ms |
17488 KB |
Output is correct |
98 |
Correct |
223 ms |
16580 KB |
Output is correct |
99 |
Correct |
261 ms |
16592 KB |
Output is correct |
100 |
Correct |
258 ms |
20192 KB |
Output is correct |
101 |
Correct |
270 ms |
17900 KB |
Output is correct |
102 |
Correct |
256 ms |
16916 KB |
Output is correct |
103 |
Correct |
261 ms |
16720 KB |
Output is correct |
104 |
Correct |
116 ms |
16896 KB |
Output is correct |
105 |
Correct |
525 ms |
27496 KB |
Output is correct |
106 |
Correct |
119 ms |
16576 KB |
Output is correct |
107 |
Correct |
113 ms |
16496 KB |
Output is correct |
108 |
Correct |
537 ms |
32336 KB |
Output is correct |
109 |
Correct |
123 ms |
16840 KB |
Output is correct |
110 |
Correct |
526 ms |
32600 KB |
Output is correct |
111 |
Correct |
230 ms |
16208 KB |
Output is correct |