#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int inf = 2e9;
vector<int> g[N];
int dp[N][2];
int str[N];
void Dfs(int s, int e) {
dp[s][0] = str[s];
dp[s][1] = -inf;
int sum = 0;
for (int u : g[s]) if (u != e) {
Dfs(u, s);
sum += dp[u][0];
smax(dp[s][1], dp[u][1] - str[s]);
if (str[s] && dp[u][0]) smax(dp[s][1], dp[u][0] + 1);
}
smax(dp[s][0], sum - str[s]);
int fs = sum;
for (int u : g[s]) if (u != e) {
if (str[s] || sum > dp[u][0]) fs += max(0, dp[u][1] - 2 - dp[u][0]);
}
smax(dp[s][0], fs - str[s]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n; cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u -= 1;
v -= 1;
g[u].push_back(v);
g[v].push_back(u);
}
string sr;
cin >> sr;
for (int i = 0; i < n; i++) str[i] = sr[i] - '0';
Dfs(0, -1);
int ans = -inf;
for (int i = 0; i < n; i++) smax(ans, max(dp[i][0], dp[i][1]));
cout << ans << en;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5020 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
5020 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4960 KB |
Output is correct |
12 |
Correct |
3 ms |
5024 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4952 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5024 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5012 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5020 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
5020 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4960 KB |
Output is correct |
12 |
Correct |
3 ms |
5024 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4952 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5024 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5012 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
5 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5040 KB |
Output is correct |
24 |
Correct |
4 ms |
5032 KB |
Output is correct |
25 |
Correct |
3 ms |
5032 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5032 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 ms |
5108 KB |
Output is correct |
32 |
Correct |
3 ms |
5032 KB |
Output is correct |
33 |
Correct |
3 ms |
5032 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5088 KB |
Output is correct |
37 |
Correct |
5 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5032 KB |
Output is correct |
39 |
Correct |
4 ms |
5204 KB |
Output is correct |
40 |
Correct |
4 ms |
5160 KB |
Output is correct |
41 |
Correct |
3 ms |
5160 KB |
Output is correct |
42 |
Correct |
4 ms |
5164 KB |
Output is correct |
43 |
Correct |
4 ms |
5076 KB |
Output is correct |
44 |
Correct |
4 ms |
5076 KB |
Output is correct |
45 |
Correct |
4 ms |
5076 KB |
Output is correct |
46 |
Correct |
4 ms |
5164 KB |
Output is correct |
47 |
Correct |
4 ms |
5084 KB |
Output is correct |
48 |
Correct |
3 ms |
5168 KB |
Output is correct |
49 |
Correct |
3 ms |
5204 KB |
Output is correct |
50 |
Correct |
3 ms |
5164 KB |
Output is correct |
51 |
Correct |
3 ms |
5076 KB |
Output is correct |
52 |
Correct |
4 ms |
5076 KB |
Output is correct |
53 |
Correct |
3 ms |
5160 KB |
Output is correct |
54 |
Correct |
4 ms |
5076 KB |
Output is correct |
55 |
Correct |
3 ms |
5076 KB |
Output is correct |
56 |
Correct |
5 ms |
5036 KB |
Output is correct |
57 |
Correct |
4 ms |
5288 KB |
Output is correct |
58 |
Correct |
3 ms |
5076 KB |
Output is correct |
59 |
Correct |
4 ms |
5072 KB |
Output is correct |
60 |
Correct |
3 ms |
5204 KB |
Output is correct |
61 |
Correct |
4 ms |
5032 KB |
Output is correct |
62 |
Correct |
4 ms |
5204 KB |
Output is correct |
63 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5020 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
5020 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4960 KB |
Output is correct |
12 |
Correct |
3 ms |
5024 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4952 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5024 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5012 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
5 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5040 KB |
Output is correct |
24 |
Correct |
4 ms |
5032 KB |
Output is correct |
25 |
Correct |
3 ms |
5032 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5032 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 ms |
5108 KB |
Output is correct |
32 |
Correct |
3 ms |
5032 KB |
Output is correct |
33 |
Correct |
3 ms |
5032 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5088 KB |
Output is correct |
37 |
Correct |
5 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5032 KB |
Output is correct |
39 |
Correct |
4 ms |
5204 KB |
Output is correct |
40 |
Correct |
4 ms |
5160 KB |
Output is correct |
41 |
Correct |
3 ms |
5160 KB |
Output is correct |
42 |
Correct |
4 ms |
5164 KB |
Output is correct |
43 |
Correct |
4 ms |
5076 KB |
Output is correct |
44 |
Correct |
4 ms |
5076 KB |
Output is correct |
45 |
Correct |
4 ms |
5076 KB |
Output is correct |
46 |
Correct |
4 ms |
5164 KB |
Output is correct |
47 |
Correct |
4 ms |
5084 KB |
Output is correct |
48 |
Correct |
3 ms |
5168 KB |
Output is correct |
49 |
Correct |
3 ms |
5204 KB |
Output is correct |
50 |
Correct |
3 ms |
5164 KB |
Output is correct |
51 |
Correct |
3 ms |
5076 KB |
Output is correct |
52 |
Correct |
4 ms |
5076 KB |
Output is correct |
53 |
Correct |
3 ms |
5160 KB |
Output is correct |
54 |
Correct |
4 ms |
5076 KB |
Output is correct |
55 |
Correct |
3 ms |
5076 KB |
Output is correct |
56 |
Correct |
5 ms |
5036 KB |
Output is correct |
57 |
Correct |
4 ms |
5288 KB |
Output is correct |
58 |
Correct |
3 ms |
5076 KB |
Output is correct |
59 |
Correct |
4 ms |
5072 KB |
Output is correct |
60 |
Correct |
3 ms |
5204 KB |
Output is correct |
61 |
Correct |
4 ms |
5032 KB |
Output is correct |
62 |
Correct |
4 ms |
5204 KB |
Output is correct |
63 |
Correct |
3 ms |
5076 KB |
Output is correct |
64 |
Correct |
119 ms |
16784 KB |
Output is correct |
65 |
Correct |
128 ms |
16672 KB |
Output is correct |
66 |
Correct |
120 ms |
16760 KB |
Output is correct |
67 |
Correct |
133 ms |
16732 KB |
Output is correct |
68 |
Correct |
120 ms |
16664 KB |
Output is correct |
69 |
Correct |
115 ms |
16768 KB |
Output is correct |
70 |
Correct |
119 ms |
16780 KB |
Output is correct |
71 |
Correct |
125 ms |
16820 KB |
Output is correct |
72 |
Correct |
120 ms |
16784 KB |
Output is correct |
73 |
Correct |
139 ms |
31864 KB |
Output is correct |
74 |
Correct |
154 ms |
31848 KB |
Output is correct |
75 |
Correct |
119 ms |
16560 KB |
Output is correct |
76 |
Correct |
113 ms |
16648 KB |
Output is correct |
77 |
Correct |
122 ms |
16572 KB |
Output is correct |
78 |
Correct |
123 ms |
16596 KB |
Output is correct |
79 |
Correct |
126 ms |
16768 KB |
Output is correct |
80 |
Correct |
137 ms |
16600 KB |
Output is correct |
81 |
Correct |
94 ms |
17088 KB |
Output is correct |
82 |
Correct |
109 ms |
16912 KB |
Output is correct |
83 |
Correct |
133 ms |
22916 KB |
Output is correct |
84 |
Correct |
162 ms |
25576 KB |
Output is correct |
85 |
Correct |
142 ms |
24996 KB |
Output is correct |
86 |
Correct |
125 ms |
18336 KB |
Output is correct |
87 |
Correct |
124 ms |
17648 KB |
Output is correct |
88 |
Correct |
134 ms |
18092 KB |
Output is correct |
89 |
Correct |
110 ms |
18084 KB |
Output is correct |
90 |
Correct |
107 ms |
17804 KB |
Output is correct |
91 |
Correct |
113 ms |
18064 KB |
Output is correct |
92 |
Correct |
121 ms |
18568 KB |
Output is correct |
93 |
Correct |
142 ms |
21472 KB |
Output is correct |
94 |
Correct |
109 ms |
23600 KB |
Output is correct |
95 |
Correct |
111 ms |
21844 KB |
Output is correct |
96 |
Correct |
113 ms |
19736 KB |
Output is correct |
97 |
Correct |
120 ms |
17948 KB |
Output is correct |
98 |
Correct |
119 ms |
17324 KB |
Output is correct |
99 |
Correct |
105 ms |
17032 KB |
Output is correct |
100 |
Correct |
135 ms |
20860 KB |
Output is correct |
101 |
Correct |
112 ms |
18624 KB |
Output is correct |
102 |
Correct |
124 ms |
17540 KB |
Output is correct |
103 |
Correct |
114 ms |
17268 KB |
Output is correct |
104 |
Correct |
71 ms |
17616 KB |
Output is correct |
105 |
Correct |
144 ms |
28112 KB |
Output is correct |
106 |
Correct |
77 ms |
17492 KB |
Output is correct |
107 |
Correct |
75 ms |
17516 KB |
Output is correct |
108 |
Correct |
147 ms |
32788 KB |
Output is correct |
109 |
Correct |
81 ms |
17576 KB |
Output is correct |
110 |
Correct |
149 ms |
33112 KB |
Output is correct |
111 |
Correct |
105 ms |
16648 KB |
Output is correct |