#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define eb emplace_back
#define pb push_back
#define sz(x) (int) (x).size()
#define mp make_pair
#define f1r(i, a, b) for (int i = (a); i < (b); ++i)
#define f0r(i, a) f1r(i, 0, a)
mt19937 rng((uint32_t) chrono::steady_clock::now().time_since_epoch().count());
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
#ifdef LOCAL
#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__);
#else
#define dbg(...) 17;
#endif
template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) { return os << "(" << p.first << ", " << p.second << ")"; }
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) { bool f = true; os << "{"; for (const auto &x : c) { if (!f) os << ", "; f = false; os << x; } return os << "}"; }
template<typename T> void debug(string s, T x) { cerr << s << " = " << x << "\n"; }
template<typename T, typename... Args> void debug(string s, T x, Args... args) { cerr << s.substr(0, s.find(',')) << " = " << x << " | "; debug(s.substr(s.find(',') + 2), args...); }
int main() {
int n;
cin >> n;
vector<vector<int>> adj(n);
f0r(i, n-1) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].pb(v);
adj[v].pb(u);
}
string s;
cin >> s;
vector<int> dp(n);
int ans = 0;
function<void(int, int)> dfs = [&](int src, int par) {
for (int nxt: adj[src]) {
if (nxt == par) continue;
dfs(nxt, src);
}
if (s[src] == '0') {
int res = 0;
for (int nxt: adj[src]) {
if (nxt == par) continue;
res += max(0, dp[nxt]);
}
ckmax(dp[src], res);
// actually updating ans, assume nothing is actually above
} else {
// no paths
dp[src] = 1;
// at least one path
// under the assumption something's above
int res = -1;
for (int nxt: adj[src]) {
res += max(0, dp[nxt]);
}
ckmax(dp[src], res);
// the node becomes negative
ckmax(ans, res);
// the node does not become negative
res = 0;
for (int nxt: adj[src]) {
if (nxt == par) continue;
ckmax(res, dp[nxt]);
}
ckmax(ans, res + 1);
}
ckmax(ans, dp[src]);
};
dfs(0, -1);
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
256 KB |
Output is correct |
21 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
256 KB |
Output is correct |
21 |
Correct |
0 ms |
256 KB |
Output is correct |
22 |
Correct |
3 ms |
512 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
512 KB |
Output is correct |
26 |
Correct |
3 ms |
512 KB |
Output is correct |
27 |
Correct |
3 ms |
512 KB |
Output is correct |
28 |
Correct |
3 ms |
512 KB |
Output is correct |
29 |
Correct |
3 ms |
640 KB |
Output is correct |
30 |
Correct |
3 ms |
512 KB |
Output is correct |
31 |
Correct |
3 ms |
512 KB |
Output is correct |
32 |
Correct |
3 ms |
512 KB |
Output is correct |
33 |
Correct |
3 ms |
512 KB |
Output is correct |
34 |
Correct |
3 ms |
512 KB |
Output is correct |
35 |
Correct |
3 ms |
512 KB |
Output is correct |
36 |
Correct |
3 ms |
512 KB |
Output is correct |
37 |
Correct |
3 ms |
512 KB |
Output is correct |
38 |
Correct |
3 ms |
512 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
3 ms |
512 KB |
Output is correct |
41 |
Correct |
3 ms |
512 KB |
Output is correct |
42 |
Correct |
3 ms |
512 KB |
Output is correct |
43 |
Correct |
3 ms |
512 KB |
Output is correct |
44 |
Correct |
3 ms |
512 KB |
Output is correct |
45 |
Correct |
3 ms |
512 KB |
Output is correct |
46 |
Correct |
41 ms |
512 KB |
Output is correct |
47 |
Correct |
3 ms |
512 KB |
Output is correct |
48 |
Correct |
3 ms |
512 KB |
Output is correct |
49 |
Correct |
3 ms |
512 KB |
Output is correct |
50 |
Correct |
3 ms |
512 KB |
Output is correct |
51 |
Correct |
3 ms |
512 KB |
Output is correct |
52 |
Correct |
3 ms |
512 KB |
Output is correct |
53 |
Correct |
3 ms |
512 KB |
Output is correct |
54 |
Correct |
3 ms |
512 KB |
Output is correct |
55 |
Correct |
3 ms |
512 KB |
Output is correct |
56 |
Correct |
3 ms |
512 KB |
Output is correct |
57 |
Correct |
3 ms |
640 KB |
Output is correct |
58 |
Correct |
3 ms |
512 KB |
Output is correct |
59 |
Correct |
4 ms |
512 KB |
Output is correct |
60 |
Correct |
3 ms |
512 KB |
Output is correct |
61 |
Correct |
3 ms |
512 KB |
Output is correct |
62 |
Correct |
3 ms |
640 KB |
Output is correct |
63 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
256 KB |
Output is correct |
21 |
Correct |
0 ms |
256 KB |
Output is correct |
22 |
Correct |
3 ms |
512 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
512 KB |
Output is correct |
26 |
Correct |
3 ms |
512 KB |
Output is correct |
27 |
Correct |
3 ms |
512 KB |
Output is correct |
28 |
Correct |
3 ms |
512 KB |
Output is correct |
29 |
Correct |
3 ms |
640 KB |
Output is correct |
30 |
Correct |
3 ms |
512 KB |
Output is correct |
31 |
Correct |
3 ms |
512 KB |
Output is correct |
32 |
Correct |
3 ms |
512 KB |
Output is correct |
33 |
Correct |
3 ms |
512 KB |
Output is correct |
34 |
Correct |
3 ms |
512 KB |
Output is correct |
35 |
Correct |
3 ms |
512 KB |
Output is correct |
36 |
Correct |
3 ms |
512 KB |
Output is correct |
37 |
Correct |
3 ms |
512 KB |
Output is correct |
38 |
Correct |
3 ms |
512 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
3 ms |
512 KB |
Output is correct |
41 |
Correct |
3 ms |
512 KB |
Output is correct |
42 |
Correct |
3 ms |
512 KB |
Output is correct |
43 |
Correct |
3 ms |
512 KB |
Output is correct |
44 |
Correct |
3 ms |
512 KB |
Output is correct |
45 |
Correct |
3 ms |
512 KB |
Output is correct |
46 |
Correct |
41 ms |
512 KB |
Output is correct |
47 |
Correct |
3 ms |
512 KB |
Output is correct |
48 |
Correct |
3 ms |
512 KB |
Output is correct |
49 |
Correct |
3 ms |
512 KB |
Output is correct |
50 |
Correct |
3 ms |
512 KB |
Output is correct |
51 |
Correct |
3 ms |
512 KB |
Output is correct |
52 |
Correct |
3 ms |
512 KB |
Output is correct |
53 |
Correct |
3 ms |
512 KB |
Output is correct |
54 |
Correct |
3 ms |
512 KB |
Output is correct |
55 |
Correct |
3 ms |
512 KB |
Output is correct |
56 |
Correct |
3 ms |
512 KB |
Output is correct |
57 |
Correct |
3 ms |
640 KB |
Output is correct |
58 |
Correct |
3 ms |
512 KB |
Output is correct |
59 |
Correct |
4 ms |
512 KB |
Output is correct |
60 |
Correct |
3 ms |
512 KB |
Output is correct |
61 |
Correct |
3 ms |
512 KB |
Output is correct |
62 |
Correct |
3 ms |
640 KB |
Output is correct |
63 |
Correct |
3 ms |
512 KB |
Output is correct |
64 |
Correct |
380 ms |
15268 KB |
Output is correct |
65 |
Correct |
380 ms |
15368 KB |
Output is correct |
66 |
Correct |
384 ms |
15364 KB |
Output is correct |
67 |
Correct |
379 ms |
15240 KB |
Output is correct |
68 |
Correct |
372 ms |
15236 KB |
Output is correct |
69 |
Correct |
374 ms |
15240 KB |
Output is correct |
70 |
Correct |
368 ms |
15240 KB |
Output is correct |
71 |
Correct |
375 ms |
15364 KB |
Output is correct |
72 |
Correct |
371 ms |
15244 KB |
Output is correct |
73 |
Correct |
411 ms |
30472 KB |
Output is correct |
74 |
Correct |
398 ms |
30344 KB |
Output is correct |
75 |
Correct |
377 ms |
15112 KB |
Output is correct |
76 |
Correct |
371 ms |
15112 KB |
Output is correct |
77 |
Correct |
381 ms |
15144 KB |
Output is correct |
78 |
Correct |
377 ms |
15112 KB |
Output is correct |
79 |
Correct |
388 ms |
15112 KB |
Output is correct |
80 |
Correct |
384 ms |
15240 KB |
Output is correct |
81 |
Correct |
343 ms |
15600 KB |
Output is correct |
82 |
Correct |
371 ms |
15476 KB |
Output is correct |
83 |
Correct |
389 ms |
21452 KB |
Output is correct |
84 |
Correct |
391 ms |
23944 KB |
Output is correct |
85 |
Correct |
385 ms |
23492 KB |
Output is correct |
86 |
Correct |
389 ms |
16776 KB |
Output is correct |
87 |
Correct |
376 ms |
16168 KB |
Output is correct |
88 |
Correct |
381 ms |
16520 KB |
Output is correct |
89 |
Correct |
367 ms |
16520 KB |
Output is correct |
90 |
Correct |
367 ms |
16392 KB |
Output is correct |
91 |
Correct |
373 ms |
16616 KB |
Output is correct |
92 |
Correct |
366 ms |
17032 KB |
Output is correct |
93 |
Correct |
351 ms |
19848 KB |
Output is correct |
94 |
Correct |
355 ms |
22144 KB |
Output is correct |
95 |
Correct |
359 ms |
20484 KB |
Output is correct |
96 |
Correct |
439 ms |
18312 KB |
Output is correct |
97 |
Correct |
389 ms |
16392 KB |
Output is correct |
98 |
Correct |
359 ms |
15752 KB |
Output is correct |
99 |
Correct |
369 ms |
15496 KB |
Output is correct |
100 |
Correct |
410 ms |
19336 KB |
Output is correct |
101 |
Correct |
374 ms |
17032 KB |
Output is correct |
102 |
Correct |
370 ms |
16136 KB |
Output is correct |
103 |
Correct |
375 ms |
15808 KB |
Output is correct |
104 |
Correct |
308 ms |
16108 KB |
Output is correct |
105 |
Correct |
391 ms |
26632 KB |
Output is correct |
106 |
Correct |
302 ms |
15852 KB |
Output is correct |
107 |
Correct |
297 ms |
15984 KB |
Output is correct |
108 |
Correct |
398 ms |
31456 KB |
Output is correct |
109 |
Correct |
309 ms |
16364 KB |
Output is correct |
110 |
Correct |
402 ms |
31680 KB |
Output is correct |
111 |
Correct |
363 ms |
15240 KB |
Output is correct |