#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5;
vector<int> g[maxn];
int dp[maxn][2];
string s;
int dfs(int v, int p) {
int res = 0;
for (const int& u : g[v]) {
if (u != p) {
res = max(res, dfs(u, v));
dp[v][0] = max(dp[v][0], max(dp[u][1], max(dp[u][0] - 2 * (s[u] - '0'), (s[u] - '0'))));
dp[v][1] += max(dp[u][1], max(dp[u][0] - 2 * (s[u] - '0'), (s[u] - '0')));
}
}
dp[v][0] += (s[v] - '0');
dp[v][1] -= (s[v] - '0');
// cerr << "v: " << v << ", 0: " << dp[v][0] << ", 1: " << dp[v][1] << '\n';
res = max(res, max(dp[v][0], dp[v][1]));
return res;
}
void solve() {
int n;
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int v, u;
cin >> v >> u;
--v, --u;
g[v].emplace_back(u);
g[u].emplace_back(v);
}
cin >> s;
cout << dfs(0, -1) << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int T = 1;
cin >> T;
while (T--) {
solve();
cerr << "-----------\n";
cout << "-----------\n";
}
#else
int T = 1;
// cin >> T;
while (T--) {
solve();
}
#endif
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6548 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6488 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6544 KB |
Output is correct |
20 |
Correct |
2 ms |
6488 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6548 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6488 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6544 KB |
Output is correct |
20 |
Correct |
2 ms |
6488 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6564 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6748 KB |
Output is correct |
30 |
Correct |
2 ms |
6492 KB |
Output is correct |
31 |
Correct |
2 ms |
6492 KB |
Output is correct |
32 |
Correct |
2 ms |
6492 KB |
Output is correct |
33 |
Correct |
2 ms |
6492 KB |
Output is correct |
34 |
Correct |
2 ms |
6492 KB |
Output is correct |
35 |
Correct |
2 ms |
6492 KB |
Output is correct |
36 |
Correct |
2 ms |
6492 KB |
Output is correct |
37 |
Correct |
2 ms |
6492 KB |
Output is correct |
38 |
Correct |
2 ms |
6492 KB |
Output is correct |
39 |
Correct |
2 ms |
6492 KB |
Output is correct |
40 |
Correct |
2 ms |
6492 KB |
Output is correct |
41 |
Correct |
2 ms |
6712 KB |
Output is correct |
42 |
Correct |
2 ms |
6492 KB |
Output is correct |
43 |
Correct |
2 ms |
6576 KB |
Output is correct |
44 |
Correct |
2 ms |
6576 KB |
Output is correct |
45 |
Correct |
2 ms |
6492 KB |
Output is correct |
46 |
Correct |
2 ms |
6492 KB |
Output is correct |
47 |
Correct |
2 ms |
6588 KB |
Output is correct |
48 |
Correct |
2 ms |
6580 KB |
Output is correct |
49 |
Correct |
2 ms |
6492 KB |
Output is correct |
50 |
Correct |
2 ms |
6580 KB |
Output is correct |
51 |
Correct |
2 ms |
6560 KB |
Output is correct |
52 |
Correct |
2 ms |
6492 KB |
Output is correct |
53 |
Correct |
2 ms |
6492 KB |
Output is correct |
54 |
Correct |
2 ms |
6492 KB |
Output is correct |
55 |
Correct |
2 ms |
6560 KB |
Output is correct |
56 |
Correct |
2 ms |
6496 KB |
Output is correct |
57 |
Correct |
2 ms |
6628 KB |
Output is correct |
58 |
Correct |
2 ms |
6492 KB |
Output is correct |
59 |
Correct |
2 ms |
6492 KB |
Output is correct |
60 |
Correct |
2 ms |
6496 KB |
Output is correct |
61 |
Correct |
2 ms |
6492 KB |
Output is correct |
62 |
Correct |
2 ms |
6752 KB |
Output is correct |
63 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6548 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6488 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6544 KB |
Output is correct |
20 |
Correct |
2 ms |
6488 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6564 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6748 KB |
Output is correct |
30 |
Correct |
2 ms |
6492 KB |
Output is correct |
31 |
Correct |
2 ms |
6492 KB |
Output is correct |
32 |
Correct |
2 ms |
6492 KB |
Output is correct |
33 |
Correct |
2 ms |
6492 KB |
Output is correct |
34 |
Correct |
2 ms |
6492 KB |
Output is correct |
35 |
Correct |
2 ms |
6492 KB |
Output is correct |
36 |
Correct |
2 ms |
6492 KB |
Output is correct |
37 |
Correct |
2 ms |
6492 KB |
Output is correct |
38 |
Correct |
2 ms |
6492 KB |
Output is correct |
39 |
Correct |
2 ms |
6492 KB |
Output is correct |
40 |
Correct |
2 ms |
6492 KB |
Output is correct |
41 |
Correct |
2 ms |
6712 KB |
Output is correct |
42 |
Correct |
2 ms |
6492 KB |
Output is correct |
43 |
Correct |
2 ms |
6576 KB |
Output is correct |
44 |
Correct |
2 ms |
6576 KB |
Output is correct |
45 |
Correct |
2 ms |
6492 KB |
Output is correct |
46 |
Correct |
2 ms |
6492 KB |
Output is correct |
47 |
Correct |
2 ms |
6588 KB |
Output is correct |
48 |
Correct |
2 ms |
6580 KB |
Output is correct |
49 |
Correct |
2 ms |
6492 KB |
Output is correct |
50 |
Correct |
2 ms |
6580 KB |
Output is correct |
51 |
Correct |
2 ms |
6560 KB |
Output is correct |
52 |
Correct |
2 ms |
6492 KB |
Output is correct |
53 |
Correct |
2 ms |
6492 KB |
Output is correct |
54 |
Correct |
2 ms |
6492 KB |
Output is correct |
55 |
Correct |
2 ms |
6560 KB |
Output is correct |
56 |
Correct |
2 ms |
6496 KB |
Output is correct |
57 |
Correct |
2 ms |
6628 KB |
Output is correct |
58 |
Correct |
2 ms |
6492 KB |
Output is correct |
59 |
Correct |
2 ms |
6492 KB |
Output is correct |
60 |
Correct |
2 ms |
6496 KB |
Output is correct |
61 |
Correct |
2 ms |
6492 KB |
Output is correct |
62 |
Correct |
2 ms |
6752 KB |
Output is correct |
63 |
Correct |
2 ms |
6492 KB |
Output is correct |
64 |
Correct |
86 ms |
16068 KB |
Output is correct |
65 |
Correct |
63 ms |
16116 KB |
Output is correct |
66 |
Correct |
60 ms |
15996 KB |
Output is correct |
67 |
Correct |
67 ms |
16044 KB |
Output is correct |
68 |
Correct |
75 ms |
16012 KB |
Output is correct |
69 |
Correct |
58 ms |
15980 KB |
Output is correct |
70 |
Correct |
64 ms |
16032 KB |
Output is correct |
71 |
Correct |
66 ms |
16028 KB |
Output is correct |
72 |
Correct |
60 ms |
16016 KB |
Output is correct |
73 |
Correct |
78 ms |
28560 KB |
Output is correct |
74 |
Correct |
97 ms |
28556 KB |
Output is correct |
75 |
Correct |
59 ms |
15928 KB |
Output is correct |
76 |
Correct |
61 ms |
15932 KB |
Output is correct |
77 |
Correct |
80 ms |
15964 KB |
Output is correct |
78 |
Correct |
61 ms |
15984 KB |
Output is correct |
79 |
Correct |
80 ms |
15944 KB |
Output is correct |
80 |
Correct |
60 ms |
15924 KB |
Output is correct |
81 |
Correct |
67 ms |
16328 KB |
Output is correct |
82 |
Correct |
66 ms |
16172 KB |
Output is correct |
83 |
Correct |
74 ms |
21144 KB |
Output is correct |
84 |
Correct |
78 ms |
23152 KB |
Output is correct |
85 |
Correct |
93 ms |
22944 KB |
Output is correct |
86 |
Correct |
66 ms |
17300 KB |
Output is correct |
87 |
Correct |
66 ms |
16692 KB |
Output is correct |
88 |
Correct |
86 ms |
17104 KB |
Output is correct |
89 |
Correct |
70 ms |
17308 KB |
Output is correct |
90 |
Correct |
71 ms |
17140 KB |
Output is correct |
91 |
Correct |
102 ms |
17148 KB |
Output is correct |
92 |
Correct |
73 ms |
17560 KB |
Output is correct |
93 |
Correct |
106 ms |
20068 KB |
Output is correct |
94 |
Correct |
82 ms |
21708 KB |
Output is correct |
95 |
Correct |
66 ms |
20488 KB |
Output is correct |
96 |
Correct |
69 ms |
18688 KB |
Output is correct |
97 |
Correct |
71 ms |
16944 KB |
Output is correct |
98 |
Correct |
66 ms |
16560 KB |
Output is correct |
99 |
Correct |
87 ms |
16900 KB |
Output is correct |
100 |
Correct |
65 ms |
19580 KB |
Output is correct |
101 |
Correct |
71 ms |
17556 KB |
Output is correct |
102 |
Correct |
63 ms |
16740 KB |
Output is correct |
103 |
Correct |
63 ms |
16564 KB |
Output is correct |
104 |
Correct |
44 ms |
16836 KB |
Output is correct |
105 |
Correct |
84 ms |
25492 KB |
Output is correct |
106 |
Correct |
43 ms |
16836 KB |
Output is correct |
107 |
Correct |
47 ms |
16840 KB |
Output is correct |
108 |
Correct |
84 ms |
29328 KB |
Output is correct |
109 |
Correct |
61 ms |
16840 KB |
Output is correct |
110 |
Correct |
84 ms |
29588 KB |
Output is correct |
111 |
Correct |
59 ms |
15936 KB |
Output is correct |