#pragma GCC optimize("O2")
#include <bits/stdc++.h>
#ifdef DEBUG
#include "debug.hpp"
#endif
using namespace std;
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define traverse(c, it) for(auto it = (c).begin(); it != (c).end(); ++it)
#define rep(i, N) for(int i = 0; i < (N); ++i)
#define rrep(i, N) for(int i = (N) - 1; i >= 0; --i)
#define rep1(i, N) for(int i = 1; i <= (N); ++i)
#define rep2(i, s, e) for(int i = (s); i <= (e); ++i)
#define rep3(i, s, e, d) for(int i = (s); (d) >= 0 ? i <= (e) : i >= (e); i += (d))
#ifdef DEBUG
#define debug(x...) { \
++dbg::depth; \
string dbg_vals = dbg::to_string(x); \
--dbg::depth; \
dbg::fprint(__func__, __LINE__, #x, dbg_vals); \
}
#define light_debug(x) { \
dbg::light = true; \
dbg::dout << __func__ << ":" << __LINE__; \
dbg::dout << " " << #x << " = " << x << endl; \
dbg::light = false; \
}
#else
#define debug(x...) 42
#define light_debug(x) 42
#endif
using ll = long long;
template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }
template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }
template<class Fun>
class y_combinator_result {
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifdef DEBUG
freopen("debug", "w", stderr);
#endif
int n; cin >> n;
vector<vector<int>> adj(n);
vector<int> dp_0(n), dp_1(n);
rep(_, n - 1) {
int a, b; cin >> a >> b; --a, --b;
adj[a].push_back(b), adj[b].push_back(a);
}
string s; cin >> s;
y_combinator([&](auto dfs, int v, int p) -> void {
for(int u : adj[v]) if(u != p) dfs(u, v);
int t{}, g{s[v] == '1'};
dp_1[v] = g;
for(int u : adj[v]) if(u != p)
t += dp_1[u];
ckmax(dp_1[v], t - g);
dp_0[v] = dp_1[v];
for(int u : adj[v]) if(u != p)
ckmax(dp_0[v], max(dp_0[u], g + dp_1[u]));
})(0, -1);
cout << dp_0[0] << '\n';
#ifdef DEBUG
dbg::dout << "\nExecution time: " << clock() * 1000 / CLOCKS_PER_SEC << "ms" << endl;
#endif
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
320 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
2 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
456 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
27 |
Correct |
2 ms |
460 KB |
Output is correct |
28 |
Correct |
2 ms |
456 KB |
Output is correct |
29 |
Correct |
2 ms |
588 KB |
Output is correct |
30 |
Correct |
2 ms |
448 KB |
Output is correct |
31 |
Correct |
2 ms |
456 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
456 KB |
Output is correct |
35 |
Correct |
2 ms |
460 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
2 ms |
460 KB |
Output is correct |
38 |
Correct |
2 ms |
460 KB |
Output is correct |
39 |
Correct |
2 ms |
460 KB |
Output is correct |
40 |
Correct |
2 ms |
464 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
2 ms |
464 KB |
Output is correct |
43 |
Correct |
2 ms |
460 KB |
Output is correct |
44 |
Correct |
2 ms |
460 KB |
Output is correct |
45 |
Correct |
2 ms |
492 KB |
Output is correct |
46 |
Correct |
2 ms |
460 KB |
Output is correct |
47 |
Correct |
2 ms |
460 KB |
Output is correct |
48 |
Correct |
2 ms |
456 KB |
Output is correct |
49 |
Correct |
2 ms |
544 KB |
Output is correct |
50 |
Correct |
2 ms |
460 KB |
Output is correct |
51 |
Correct |
2 ms |
460 KB |
Output is correct |
52 |
Correct |
2 ms |
460 KB |
Output is correct |
53 |
Correct |
2 ms |
460 KB |
Output is correct |
54 |
Correct |
1 ms |
460 KB |
Output is correct |
55 |
Correct |
2 ms |
464 KB |
Output is correct |
56 |
Correct |
2 ms |
456 KB |
Output is correct |
57 |
Correct |
2 ms |
588 KB |
Output is correct |
58 |
Correct |
2 ms |
456 KB |
Output is correct |
59 |
Correct |
2 ms |
452 KB |
Output is correct |
60 |
Correct |
2 ms |
460 KB |
Output is correct |
61 |
Correct |
2 ms |
460 KB |
Output is correct |
62 |
Correct |
2 ms |
460 KB |
Output is correct |
63 |
Correct |
2 ms |
452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
320 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
2 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
456 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
27 |
Correct |
2 ms |
460 KB |
Output is correct |
28 |
Correct |
2 ms |
456 KB |
Output is correct |
29 |
Correct |
2 ms |
588 KB |
Output is correct |
30 |
Correct |
2 ms |
448 KB |
Output is correct |
31 |
Correct |
2 ms |
456 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
456 KB |
Output is correct |
35 |
Correct |
2 ms |
460 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
2 ms |
460 KB |
Output is correct |
38 |
Correct |
2 ms |
460 KB |
Output is correct |
39 |
Correct |
2 ms |
460 KB |
Output is correct |
40 |
Correct |
2 ms |
464 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
2 ms |
464 KB |
Output is correct |
43 |
Correct |
2 ms |
460 KB |
Output is correct |
44 |
Correct |
2 ms |
460 KB |
Output is correct |
45 |
Correct |
2 ms |
492 KB |
Output is correct |
46 |
Correct |
2 ms |
460 KB |
Output is correct |
47 |
Correct |
2 ms |
460 KB |
Output is correct |
48 |
Correct |
2 ms |
456 KB |
Output is correct |
49 |
Correct |
2 ms |
544 KB |
Output is correct |
50 |
Correct |
2 ms |
460 KB |
Output is correct |
51 |
Correct |
2 ms |
460 KB |
Output is correct |
52 |
Correct |
2 ms |
460 KB |
Output is correct |
53 |
Correct |
2 ms |
460 KB |
Output is correct |
54 |
Correct |
1 ms |
460 KB |
Output is correct |
55 |
Correct |
2 ms |
464 KB |
Output is correct |
56 |
Correct |
2 ms |
456 KB |
Output is correct |
57 |
Correct |
2 ms |
588 KB |
Output is correct |
58 |
Correct |
2 ms |
456 KB |
Output is correct |
59 |
Correct |
2 ms |
452 KB |
Output is correct |
60 |
Correct |
2 ms |
460 KB |
Output is correct |
61 |
Correct |
2 ms |
460 KB |
Output is correct |
62 |
Correct |
2 ms |
460 KB |
Output is correct |
63 |
Correct |
2 ms |
452 KB |
Output is correct |
64 |
Correct |
143 ms |
16000 KB |
Output is correct |
65 |
Correct |
151 ms |
16100 KB |
Output is correct |
66 |
Correct |
149 ms |
16112 KB |
Output is correct |
67 |
Correct |
145 ms |
16032 KB |
Output is correct |
68 |
Correct |
168 ms |
16024 KB |
Output is correct |
69 |
Correct |
175 ms |
16016 KB |
Output is correct |
70 |
Correct |
144 ms |
16044 KB |
Output is correct |
71 |
Correct |
197 ms |
15988 KB |
Output is correct |
72 |
Correct |
155 ms |
16092 KB |
Output is correct |
73 |
Correct |
163 ms |
28540 KB |
Output is correct |
74 |
Correct |
166 ms |
28500 KB |
Output is correct |
75 |
Correct |
153 ms |
15908 KB |
Output is correct |
76 |
Correct |
150 ms |
16044 KB |
Output is correct |
77 |
Correct |
154 ms |
15988 KB |
Output is correct |
78 |
Correct |
174 ms |
16012 KB |
Output is correct |
79 |
Correct |
157 ms |
15912 KB |
Output is correct |
80 |
Correct |
156 ms |
16128 KB |
Output is correct |
81 |
Correct |
127 ms |
16304 KB |
Output is correct |
82 |
Correct |
143 ms |
16068 KB |
Output is correct |
83 |
Correct |
157 ms |
21124 KB |
Output is correct |
84 |
Correct |
167 ms |
23120 KB |
Output is correct |
85 |
Correct |
178 ms |
22804 KB |
Output is correct |
86 |
Correct |
144 ms |
17304 KB |
Output is correct |
87 |
Correct |
154 ms |
16728 KB |
Output is correct |
88 |
Correct |
157 ms |
17088 KB |
Output is correct |
89 |
Correct |
134 ms |
17216 KB |
Output is correct |
90 |
Correct |
137 ms |
17016 KB |
Output is correct |
91 |
Correct |
161 ms |
17220 KB |
Output is correct |
92 |
Correct |
145 ms |
17564 KB |
Output is correct |
93 |
Correct |
137 ms |
19812 KB |
Output is correct |
94 |
Correct |
158 ms |
21640 KB |
Output is correct |
95 |
Correct |
145 ms |
20352 KB |
Output is correct |
96 |
Correct |
138 ms |
18568 KB |
Output is correct |
97 |
Correct |
153 ms |
16948 KB |
Output is correct |
98 |
Correct |
128 ms |
16468 KB |
Output is correct |
99 |
Correct |
136 ms |
16356 KB |
Output is correct |
100 |
Correct |
141 ms |
19528 KB |
Output is correct |
101 |
Correct |
171 ms |
17448 KB |
Output is correct |
102 |
Correct |
145 ms |
16668 KB |
Output is correct |
103 |
Correct |
166 ms |
16564 KB |
Output is correct |
104 |
Correct |
124 ms |
16808 KB |
Output is correct |
105 |
Correct |
162 ms |
25348 KB |
Output is correct |
106 |
Correct |
93 ms |
16708 KB |
Output is correct |
107 |
Correct |
88 ms |
16724 KB |
Output is correct |
108 |
Correct |
184 ms |
29324 KB |
Output is correct |
109 |
Correct |
90 ms |
16856 KB |
Output is correct |
110 |
Correct |
172 ms |
29604 KB |
Output is correct |
111 |
Correct |
130 ms |
15888 KB |
Output is correct |