#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set>
typedef long long llong;
const int MAXN = 2e5 + 10;
const int INF = 2e9;
int n;
int a[MAXN];
int b[MAXN];
int sz[MAXN];
char s[MAXN];
int dp[MAXN][2];
bool bl[MAXN][2];
std::vector <int> g[MAXN];
void buildDFS(int node, int par)
{
for (int &u : g[node])
{
if (u == par)
{
std::swap(u, g[node].back());
g[node].pop_back();
break;
}
}
sz[node] = s[node] - '0';
for (const int &u : g[node])
{
buildDFS(u, node);
sz[node] += sz[u];
}
}
int f(int node, bool parentHas)
{
if (bl[node][parentHas])
{
return dp[node][parentHas];
}
bl[node][parentHas] = true;
dp[node][parentHas] = std::max(dp[node][parentHas], (int)(s[node] == '1'));
if (!parentHas && s[node] == '1')
{
for (const int &u : g[node])
{
dp[node][parentHas] = std::max(dp[node][parentHas], f(u, true) + 1);
}
}
if (!parentHas)
{
for (const int &u : g[node])
{
dp[node][parentHas] = std::max(dp[node][parentHas], f(u, false));
}
}
int curr = 0;
for (const int &u : g[node])
{
curr += f(u, true);
}
dp[node][parentHas] = std::max(dp[node][parentHas], curr - (s[node] == '1'));
return dp[node][parentHas];
}
void solve()
{
buildDFS(1, 0);
std::cout << f(1, 0) << '\n';
}
void input()
{
std::cin >> n;
for (int i = 1 ; i < n ; ++i)
{
int u, v;
std::cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
std::cin >> s + 1;
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
Compilation message
power.cpp: In function 'void input()':
power.cpp:97:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
97 | std::cin >> s + 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8792 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8800 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
21 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8792 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8800 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
21 |
Correct |
1 ms |
8796 KB |
Output is correct |
22 |
Correct |
3 ms |
8796 KB |
Output is correct |
23 |
Correct |
3 ms |
8920 KB |
Output is correct |
24 |
Correct |
3 ms |
8792 KB |
Output is correct |
25 |
Correct |
3 ms |
8860 KB |
Output is correct |
26 |
Correct |
2 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
3 ms |
8796 KB |
Output is correct |
29 |
Correct |
2 ms |
8796 KB |
Output is correct |
30 |
Correct |
2 ms |
8796 KB |
Output is correct |
31 |
Correct |
2 ms |
8796 KB |
Output is correct |
32 |
Correct |
2 ms |
8796 KB |
Output is correct |
33 |
Correct |
2 ms |
8796 KB |
Output is correct |
34 |
Correct |
2 ms |
8796 KB |
Output is correct |
35 |
Correct |
2 ms |
8796 KB |
Output is correct |
36 |
Correct |
2 ms |
8792 KB |
Output is correct |
37 |
Correct |
3 ms |
8796 KB |
Output is correct |
38 |
Correct |
2 ms |
8796 KB |
Output is correct |
39 |
Correct |
3 ms |
8836 KB |
Output is correct |
40 |
Correct |
3 ms |
8852 KB |
Output is correct |
41 |
Correct |
3 ms |
8796 KB |
Output is correct |
42 |
Correct |
3 ms |
8796 KB |
Output is correct |
43 |
Correct |
2 ms |
8796 KB |
Output is correct |
44 |
Correct |
3 ms |
8796 KB |
Output is correct |
45 |
Correct |
2 ms |
8796 KB |
Output is correct |
46 |
Correct |
2 ms |
8796 KB |
Output is correct |
47 |
Correct |
2 ms |
8792 KB |
Output is correct |
48 |
Correct |
2 ms |
8796 KB |
Output is correct |
49 |
Correct |
2 ms |
8856 KB |
Output is correct |
50 |
Correct |
3 ms |
8796 KB |
Output is correct |
51 |
Correct |
2 ms |
8984 KB |
Output is correct |
52 |
Correct |
2 ms |
9108 KB |
Output is correct |
53 |
Correct |
2 ms |
8796 KB |
Output is correct |
54 |
Correct |
2 ms |
8796 KB |
Output is correct |
55 |
Correct |
2 ms |
8796 KB |
Output is correct |
56 |
Correct |
2 ms |
8796 KB |
Output is correct |
57 |
Correct |
2 ms |
8796 KB |
Output is correct |
58 |
Correct |
2 ms |
8796 KB |
Output is correct |
59 |
Correct |
2 ms |
8796 KB |
Output is correct |
60 |
Correct |
2 ms |
8856 KB |
Output is correct |
61 |
Correct |
2 ms |
8796 KB |
Output is correct |
62 |
Correct |
2 ms |
8796 KB |
Output is correct |
63 |
Correct |
3 ms |
8792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8792 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8800 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
21 |
Correct |
1 ms |
8796 KB |
Output is correct |
22 |
Correct |
3 ms |
8796 KB |
Output is correct |
23 |
Correct |
3 ms |
8920 KB |
Output is correct |
24 |
Correct |
3 ms |
8792 KB |
Output is correct |
25 |
Correct |
3 ms |
8860 KB |
Output is correct |
26 |
Correct |
2 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
3 ms |
8796 KB |
Output is correct |
29 |
Correct |
2 ms |
8796 KB |
Output is correct |
30 |
Correct |
2 ms |
8796 KB |
Output is correct |
31 |
Correct |
2 ms |
8796 KB |
Output is correct |
32 |
Correct |
2 ms |
8796 KB |
Output is correct |
33 |
Correct |
2 ms |
8796 KB |
Output is correct |
34 |
Correct |
2 ms |
8796 KB |
Output is correct |
35 |
Correct |
2 ms |
8796 KB |
Output is correct |
36 |
Correct |
2 ms |
8792 KB |
Output is correct |
37 |
Correct |
3 ms |
8796 KB |
Output is correct |
38 |
Correct |
2 ms |
8796 KB |
Output is correct |
39 |
Correct |
3 ms |
8836 KB |
Output is correct |
40 |
Correct |
3 ms |
8852 KB |
Output is correct |
41 |
Correct |
3 ms |
8796 KB |
Output is correct |
42 |
Correct |
3 ms |
8796 KB |
Output is correct |
43 |
Correct |
2 ms |
8796 KB |
Output is correct |
44 |
Correct |
3 ms |
8796 KB |
Output is correct |
45 |
Correct |
2 ms |
8796 KB |
Output is correct |
46 |
Correct |
2 ms |
8796 KB |
Output is correct |
47 |
Correct |
2 ms |
8792 KB |
Output is correct |
48 |
Correct |
2 ms |
8796 KB |
Output is correct |
49 |
Correct |
2 ms |
8856 KB |
Output is correct |
50 |
Correct |
3 ms |
8796 KB |
Output is correct |
51 |
Correct |
2 ms |
8984 KB |
Output is correct |
52 |
Correct |
2 ms |
9108 KB |
Output is correct |
53 |
Correct |
2 ms |
8796 KB |
Output is correct |
54 |
Correct |
2 ms |
8796 KB |
Output is correct |
55 |
Correct |
2 ms |
8796 KB |
Output is correct |
56 |
Correct |
2 ms |
8796 KB |
Output is correct |
57 |
Correct |
2 ms |
8796 KB |
Output is correct |
58 |
Correct |
2 ms |
8796 KB |
Output is correct |
59 |
Correct |
2 ms |
8796 KB |
Output is correct |
60 |
Correct |
2 ms |
8856 KB |
Output is correct |
61 |
Correct |
2 ms |
8796 KB |
Output is correct |
62 |
Correct |
2 ms |
8796 KB |
Output is correct |
63 |
Correct |
3 ms |
8792 KB |
Output is correct |
64 |
Correct |
77 ms |
17752 KB |
Output is correct |
65 |
Correct |
77 ms |
17792 KB |
Output is correct |
66 |
Correct |
80 ms |
17748 KB |
Output is correct |
67 |
Correct |
79 ms |
17912 KB |
Output is correct |
68 |
Correct |
93 ms |
18196 KB |
Output is correct |
69 |
Correct |
92 ms |
17748 KB |
Output is correct |
70 |
Correct |
108 ms |
17908 KB |
Output is correct |
71 |
Correct |
72 ms |
17752 KB |
Output is correct |
72 |
Correct |
69 ms |
17748 KB |
Output is correct |
73 |
Correct |
141 ms |
27900 KB |
Output is correct |
74 |
Correct |
142 ms |
27732 KB |
Output is correct |
75 |
Correct |
74 ms |
17612 KB |
Output is correct |
76 |
Correct |
103 ms |
17684 KB |
Output is correct |
77 |
Correct |
88 ms |
17696 KB |
Output is correct |
78 |
Correct |
129 ms |
17732 KB |
Output is correct |
79 |
Correct |
75 ms |
17748 KB |
Output is correct |
80 |
Correct |
72 ms |
17800 KB |
Output is correct |
81 |
Correct |
67 ms |
18124 KB |
Output is correct |
82 |
Correct |
81 ms |
17928 KB |
Output is correct |
83 |
Correct |
128 ms |
21924 KB |
Output is correct |
84 |
Correct |
153 ms |
23700 KB |
Output is correct |
85 |
Correct |
99 ms |
23376 KB |
Output is correct |
86 |
Correct |
113 ms |
18768 KB |
Output is correct |
87 |
Correct |
79 ms |
18516 KB |
Output is correct |
88 |
Correct |
108 ms |
18976 KB |
Output is correct |
89 |
Correct |
101 ms |
18868 KB |
Output is correct |
90 |
Correct |
129 ms |
18836 KB |
Output is correct |
91 |
Correct |
82 ms |
19028 KB |
Output is correct |
92 |
Correct |
110 ms |
19300 KB |
Output is correct |
93 |
Correct |
107 ms |
20892 KB |
Output is correct |
94 |
Correct |
104 ms |
22392 KB |
Output is correct |
95 |
Correct |
115 ms |
21364 KB |
Output is correct |
96 |
Correct |
83 ms |
20052 KB |
Output is correct |
97 |
Correct |
70 ms |
18772 KB |
Output is correct |
98 |
Correct |
93 ms |
18448 KB |
Output is correct |
99 |
Correct |
104 ms |
18132 KB |
Output is correct |
100 |
Correct |
84 ms |
20600 KB |
Output is correct |
101 |
Correct |
78 ms |
18936 KB |
Output is correct |
102 |
Correct |
74 ms |
18516 KB |
Output is correct |
103 |
Correct |
118 ms |
18280 KB |
Output is correct |
104 |
Correct |
63 ms |
18632 KB |
Output is correct |
105 |
Correct |
102 ms |
25424 KB |
Output is correct |
106 |
Correct |
54 ms |
18376 KB |
Output is correct |
107 |
Correct |
49 ms |
18372 KB |
Output is correct |
108 |
Correct |
125 ms |
28504 KB |
Output is correct |
109 |
Correct |
58 ms |
18488 KB |
Output is correct |
110 |
Correct |
121 ms |
28612 KB |
Output is correct |
111 |
Correct |
62 ms |
17744 KB |
Output is correct |