#include <bits/stdc++.h>
using namespace std;
const int O = 2e5 + 5;
const int N = (1 << 10) + 5;
const int mod = 1e9 + 7; //998244353;
const int inf = 1e9;
int pr[] = {167772161, 469762049, 754974721, 1045430273, 1051721729, 1053818881};
int n, f[2][2][O];
vector <int> g[O];
bool a[O];
void dfs(int u, int par = 0){
int dif0 = 0, dif1 = 0;
for (int v : g[u]){
if (v != par){
dfs(v, u);
f[0][0][u] += f[0][0][v];
f[1][1][u] += max({f[1][1][v], f[0][0][v], f[1][0][v]});
f[0][1][u] += f[0][0][v];
dif0 = max(dif0, f[0][1][v] - f[0][0][v]);
dif1 = max(dif1, max(f[1][0][v], f[1][1][v]) - f[0][0][v]);
}
}
f[1][0][u] += a[u];
f[1][1][u] -= a[u];
f[0][1][u] = f[0][1][u] + max(max((int)a[u], dif0), a[u] + dif1);
}
main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i < n; ++ i){
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; ++ i){
char x; cin >> x;
a[i] = (x == '1');
}
dfs(1);
int res = 0;
for (int i = 0; i < 2; ++ i){
for (int j = 0; j < 2; ++ j){
//if (!(i == 1 && j == 0)){
//for (int z = 1; z <= n; ++ z) cout << i << " " << j << " " << z << " = " << f[i][j][z] << endl;
//}
res = max(res, f[i][j][1]);
}
}
cout << res;
}
/**
5
1 2
2 3
3 4
4 5
2
1 1
2 1
**/
Compilation message
power.cpp:32:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
32 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6620 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6624 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6620 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6624 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6628 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6748 KB |
Output is correct |
30 |
Correct |
2 ms |
6748 KB |
Output is correct |
31 |
Correct |
2 ms |
6492 KB |
Output is correct |
32 |
Correct |
2 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
6700 KB |
Output is correct |
35 |
Correct |
1 ms |
6492 KB |
Output is correct |
36 |
Correct |
1 ms |
6672 KB |
Output is correct |
37 |
Correct |
2 ms |
6488 KB |
Output is correct |
38 |
Correct |
1 ms |
6488 KB |
Output is correct |
39 |
Correct |
1 ms |
6748 KB |
Output is correct |
40 |
Correct |
1 ms |
6748 KB |
Output is correct |
41 |
Correct |
2 ms |
6748 KB |
Output is correct |
42 |
Correct |
1 ms |
6700 KB |
Output is correct |
43 |
Correct |
1 ms |
6744 KB |
Output is correct |
44 |
Correct |
1 ms |
6680 KB |
Output is correct |
45 |
Correct |
1 ms |
6748 KB |
Output is correct |
46 |
Correct |
2 ms |
6744 KB |
Output is correct |
47 |
Correct |
1 ms |
6748 KB |
Output is correct |
48 |
Correct |
1 ms |
6576 KB |
Output is correct |
49 |
Correct |
2 ms |
6748 KB |
Output is correct |
50 |
Correct |
2 ms |
6748 KB |
Output is correct |
51 |
Correct |
2 ms |
6744 KB |
Output is correct |
52 |
Correct |
2 ms |
6492 KB |
Output is correct |
53 |
Correct |
1 ms |
6748 KB |
Output is correct |
54 |
Correct |
1 ms |
6748 KB |
Output is correct |
55 |
Correct |
1 ms |
6668 KB |
Output is correct |
56 |
Correct |
1 ms |
6492 KB |
Output is correct |
57 |
Correct |
2 ms |
6748 KB |
Output is correct |
58 |
Correct |
2 ms |
6488 KB |
Output is correct |
59 |
Correct |
2 ms |
6492 KB |
Output is correct |
60 |
Correct |
1 ms |
6748 KB |
Output is correct |
61 |
Correct |
2 ms |
6632 KB |
Output is correct |
62 |
Correct |
2 ms |
6748 KB |
Output is correct |
63 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6620 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6624 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6628 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6748 KB |
Output is correct |
30 |
Correct |
2 ms |
6748 KB |
Output is correct |
31 |
Correct |
2 ms |
6492 KB |
Output is correct |
32 |
Correct |
2 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
6700 KB |
Output is correct |
35 |
Correct |
1 ms |
6492 KB |
Output is correct |
36 |
Correct |
1 ms |
6672 KB |
Output is correct |
37 |
Correct |
2 ms |
6488 KB |
Output is correct |
38 |
Correct |
1 ms |
6488 KB |
Output is correct |
39 |
Correct |
1 ms |
6748 KB |
Output is correct |
40 |
Correct |
1 ms |
6748 KB |
Output is correct |
41 |
Correct |
2 ms |
6748 KB |
Output is correct |
42 |
Correct |
1 ms |
6700 KB |
Output is correct |
43 |
Correct |
1 ms |
6744 KB |
Output is correct |
44 |
Correct |
1 ms |
6680 KB |
Output is correct |
45 |
Correct |
1 ms |
6748 KB |
Output is correct |
46 |
Correct |
2 ms |
6744 KB |
Output is correct |
47 |
Correct |
1 ms |
6748 KB |
Output is correct |
48 |
Correct |
1 ms |
6576 KB |
Output is correct |
49 |
Correct |
2 ms |
6748 KB |
Output is correct |
50 |
Correct |
2 ms |
6748 KB |
Output is correct |
51 |
Correct |
2 ms |
6744 KB |
Output is correct |
52 |
Correct |
2 ms |
6492 KB |
Output is correct |
53 |
Correct |
1 ms |
6748 KB |
Output is correct |
54 |
Correct |
1 ms |
6748 KB |
Output is correct |
55 |
Correct |
1 ms |
6668 KB |
Output is correct |
56 |
Correct |
1 ms |
6492 KB |
Output is correct |
57 |
Correct |
2 ms |
6748 KB |
Output is correct |
58 |
Correct |
2 ms |
6488 KB |
Output is correct |
59 |
Correct |
2 ms |
6492 KB |
Output is correct |
60 |
Correct |
1 ms |
6748 KB |
Output is correct |
61 |
Correct |
2 ms |
6632 KB |
Output is correct |
62 |
Correct |
2 ms |
6748 KB |
Output is correct |
63 |
Correct |
1 ms |
6492 KB |
Output is correct |
64 |
Correct |
57 ms |
17492 KB |
Output is correct |
65 |
Correct |
65 ms |
17384 KB |
Output is correct |
66 |
Correct |
62 ms |
17380 KB |
Output is correct |
67 |
Correct |
57 ms |
17492 KB |
Output is correct |
68 |
Correct |
51 ms |
17372 KB |
Output is correct |
69 |
Correct |
72 ms |
17488 KB |
Output is correct |
70 |
Correct |
54 ms |
17400 KB |
Output is correct |
71 |
Correct |
51 ms |
17500 KB |
Output is correct |
72 |
Correct |
53 ms |
17644 KB |
Output is correct |
73 |
Correct |
62 ms |
32592 KB |
Output is correct |
74 |
Correct |
62 ms |
32592 KB |
Output is correct |
75 |
Correct |
54 ms |
17408 KB |
Output is correct |
76 |
Correct |
53 ms |
17236 KB |
Output is correct |
77 |
Correct |
57 ms |
17236 KB |
Output is correct |
78 |
Correct |
51 ms |
17232 KB |
Output is correct |
79 |
Correct |
59 ms |
17236 KB |
Output is correct |
80 |
Correct |
54 ms |
17236 KB |
Output is correct |
81 |
Correct |
50 ms |
17612 KB |
Output is correct |
82 |
Correct |
49 ms |
17436 KB |
Output is correct |
83 |
Correct |
57 ms |
23632 KB |
Output is correct |
84 |
Correct |
63 ms |
26196 KB |
Output is correct |
85 |
Correct |
64 ms |
25632 KB |
Output is correct |
86 |
Correct |
53 ms |
19028 KB |
Output is correct |
87 |
Correct |
58 ms |
18256 KB |
Output is correct |
88 |
Correct |
56 ms |
18768 KB |
Output is correct |
89 |
Correct |
53 ms |
18768 KB |
Output is correct |
90 |
Correct |
56 ms |
18516 KB |
Output is correct |
91 |
Correct |
53 ms |
18856 KB |
Output is correct |
92 |
Correct |
56 ms |
19284 KB |
Output is correct |
93 |
Correct |
53 ms |
22220 KB |
Output is correct |
94 |
Correct |
57 ms |
24264 KB |
Output is correct |
95 |
Correct |
53 ms |
22732 KB |
Output is correct |
96 |
Correct |
62 ms |
20560 KB |
Output is correct |
97 |
Correct |
51 ms |
18536 KB |
Output is correct |
98 |
Correct |
58 ms |
18004 KB |
Output is correct |
99 |
Correct |
49 ms |
17660 KB |
Output is correct |
100 |
Correct |
56 ms |
21720 KB |
Output is correct |
101 |
Correct |
53 ms |
19060 KB |
Output is correct |
102 |
Correct |
49 ms |
18260 KB |
Output is correct |
103 |
Correct |
52 ms |
17996 KB |
Output is correct |
104 |
Correct |
38 ms |
18164 KB |
Output is correct |
105 |
Correct |
73 ms |
28752 KB |
Output is correct |
106 |
Correct |
41 ms |
18116 KB |
Output is correct |
107 |
Correct |
37 ms |
17924 KB |
Output is correct |
108 |
Correct |
61 ms |
33364 KB |
Output is correct |
109 |
Correct |
45 ms |
18184 KB |
Output is correct |
110 |
Correct |
62 ms |
33872 KB |
Output is correct |
111 |
Correct |
48 ms |
17228 KB |
Output is correct |