#include <bits/stdc++.h>
using namespace std;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
using ll = long long;
const ll mod = 1e9+7;
const int maxn = 200000 + 100;
// Assume answer is rooted at node and join the children, test all nodes via re-rooting trick.
// Key observation: a node's contribution upwards is either itself, or disjoint children in it's subtrees.
int n;
vector<int> g[maxn];
string s;
struct vec {
multiset<int> ms;
int tot=0;
void insert(int x) {
ms.insert(x);
tot += x;
}
void erase(int x) {
ms.erase(ms.find(x));
tot -= x;
}
int sum() {
return tot;
}
bool empty() {
return ms.empty();
}
int max() {
return *ms.rbegin();
}
};
vec dp[maxn];
int cache[maxn];
int dfs(int at, int p) {
for (int to: g[at]) {
if (to==p) continue;
int rec=dfs(to,at);
if (rec>0) {
dp[at].insert(rec);
}
}
int res=int(s[at]-'0'); // self
if (!dp[at].empty()) {
res=max(res, dp[at].sum()-int(s[at]-'0'));
}
return cache[at]=res;
}
int best=0;
void solve(int at, int p) {
// compute subtree[at]
int cur = int(s[at]-'0');
if (!dp[at].empty()) {
if (s[at]=='1') {
best = max(best, 1+dp[at].max());
}
cur = max(cur, dp[at].sum()-int(s[at]-'0'));
}
best = max(best, cur);
////////////////////////////////
for (int to: g[at]) {
if (to != p) {
if (cache[to]) {
dp[at].erase(cache[to]);
}
int fromP = int(s[at]-'0');
if (!dp[at].empty()) {
fromP = max(fromP, dp[at].sum()-int(s[at]-'0'));
}
//cout<<at+1<<"->"<<to+1<<": "<<fromP<<endl;
dp[to].insert(fromP);
solve(to, at);
dp[to].erase(fromP);
if (cache[to]) {
dp[at].insert(cache[to]);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n;
for (int i=0; i<n-1; i++) {
int u,v; cin>>u>>v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
cin>>s;
dfs(0,-1);
solve(0,-1);
cout<<best<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16000 KB |
Output is correct |
2 |
Correct |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
11 ms |
16000 KB |
Output is correct |
4 |
Correct |
11 ms |
16000 KB |
Output is correct |
5 |
Correct |
11 ms |
16000 KB |
Output is correct |
6 |
Correct |
11 ms |
16000 KB |
Output is correct |
7 |
Correct |
12 ms |
16000 KB |
Output is correct |
8 |
Correct |
12 ms |
16000 KB |
Output is correct |
9 |
Correct |
12 ms |
16000 KB |
Output is correct |
10 |
Correct |
11 ms |
16000 KB |
Output is correct |
11 |
Correct |
11 ms |
16000 KB |
Output is correct |
12 |
Correct |
11 ms |
16000 KB |
Output is correct |
13 |
Correct |
11 ms |
16000 KB |
Output is correct |
14 |
Correct |
11 ms |
16000 KB |
Output is correct |
15 |
Correct |
11 ms |
16000 KB |
Output is correct |
16 |
Correct |
11 ms |
16000 KB |
Output is correct |
17 |
Correct |
11 ms |
16000 KB |
Output is correct |
18 |
Correct |
11 ms |
16000 KB |
Output is correct |
19 |
Correct |
12 ms |
16000 KB |
Output is correct |
20 |
Correct |
11 ms |
16000 KB |
Output is correct |
21 |
Correct |
11 ms |
16000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16000 KB |
Output is correct |
2 |
Correct |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
11 ms |
16000 KB |
Output is correct |
4 |
Correct |
11 ms |
16000 KB |
Output is correct |
5 |
Correct |
11 ms |
16000 KB |
Output is correct |
6 |
Correct |
11 ms |
16000 KB |
Output is correct |
7 |
Correct |
12 ms |
16000 KB |
Output is correct |
8 |
Correct |
12 ms |
16000 KB |
Output is correct |
9 |
Correct |
12 ms |
16000 KB |
Output is correct |
10 |
Correct |
11 ms |
16000 KB |
Output is correct |
11 |
Correct |
11 ms |
16000 KB |
Output is correct |
12 |
Correct |
11 ms |
16000 KB |
Output is correct |
13 |
Correct |
11 ms |
16000 KB |
Output is correct |
14 |
Correct |
11 ms |
16000 KB |
Output is correct |
15 |
Correct |
11 ms |
16000 KB |
Output is correct |
16 |
Correct |
11 ms |
16000 KB |
Output is correct |
17 |
Correct |
11 ms |
16000 KB |
Output is correct |
18 |
Correct |
11 ms |
16000 KB |
Output is correct |
19 |
Correct |
12 ms |
16000 KB |
Output is correct |
20 |
Correct |
11 ms |
16000 KB |
Output is correct |
21 |
Correct |
11 ms |
16000 KB |
Output is correct |
22 |
Correct |
12 ms |
16256 KB |
Output is correct |
23 |
Correct |
12 ms |
16128 KB |
Output is correct |
24 |
Correct |
13 ms |
16128 KB |
Output is correct |
25 |
Correct |
12 ms |
16128 KB |
Output is correct |
26 |
Correct |
12 ms |
16128 KB |
Output is correct |
27 |
Correct |
12 ms |
16128 KB |
Output is correct |
28 |
Correct |
12 ms |
16128 KB |
Output is correct |
29 |
Correct |
12 ms |
16384 KB |
Output is correct |
30 |
Correct |
13 ms |
16256 KB |
Output is correct |
31 |
Correct |
13 ms |
16128 KB |
Output is correct |
32 |
Correct |
12 ms |
16128 KB |
Output is correct |
33 |
Correct |
12 ms |
16128 KB |
Output is correct |
34 |
Correct |
12 ms |
16128 KB |
Output is correct |
35 |
Correct |
12 ms |
16128 KB |
Output is correct |
36 |
Correct |
12 ms |
16128 KB |
Output is correct |
37 |
Correct |
13 ms |
16128 KB |
Output is correct |
38 |
Correct |
12 ms |
16128 KB |
Output is correct |
39 |
Correct |
13 ms |
16256 KB |
Output is correct |
40 |
Correct |
12 ms |
16256 KB |
Output is correct |
41 |
Correct |
12 ms |
16256 KB |
Output is correct |
42 |
Correct |
12 ms |
16128 KB |
Output is correct |
43 |
Correct |
12 ms |
16128 KB |
Output is correct |
44 |
Correct |
13 ms |
16256 KB |
Output is correct |
45 |
Correct |
13 ms |
16128 KB |
Output is correct |
46 |
Correct |
12 ms |
16256 KB |
Output is correct |
47 |
Correct |
12 ms |
16128 KB |
Output is correct |
48 |
Correct |
13 ms |
16256 KB |
Output is correct |
49 |
Correct |
12 ms |
16256 KB |
Output is correct |
50 |
Correct |
12 ms |
16256 KB |
Output is correct |
51 |
Correct |
12 ms |
16256 KB |
Output is correct |
52 |
Correct |
14 ms |
16128 KB |
Output is correct |
53 |
Correct |
12 ms |
16128 KB |
Output is correct |
54 |
Correct |
13 ms |
16256 KB |
Output is correct |
55 |
Correct |
13 ms |
16128 KB |
Output is correct |
56 |
Correct |
12 ms |
16128 KB |
Output is correct |
57 |
Correct |
13 ms |
16384 KB |
Output is correct |
58 |
Correct |
12 ms |
16160 KB |
Output is correct |
59 |
Correct |
15 ms |
16128 KB |
Output is correct |
60 |
Correct |
12 ms |
16256 KB |
Output is correct |
61 |
Correct |
12 ms |
16128 KB |
Output is correct |
62 |
Correct |
12 ms |
16256 KB |
Output is correct |
63 |
Correct |
12 ms |
16128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16000 KB |
Output is correct |
2 |
Correct |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
11 ms |
16000 KB |
Output is correct |
4 |
Correct |
11 ms |
16000 KB |
Output is correct |
5 |
Correct |
11 ms |
16000 KB |
Output is correct |
6 |
Correct |
11 ms |
16000 KB |
Output is correct |
7 |
Correct |
12 ms |
16000 KB |
Output is correct |
8 |
Correct |
12 ms |
16000 KB |
Output is correct |
9 |
Correct |
12 ms |
16000 KB |
Output is correct |
10 |
Correct |
11 ms |
16000 KB |
Output is correct |
11 |
Correct |
11 ms |
16000 KB |
Output is correct |
12 |
Correct |
11 ms |
16000 KB |
Output is correct |
13 |
Correct |
11 ms |
16000 KB |
Output is correct |
14 |
Correct |
11 ms |
16000 KB |
Output is correct |
15 |
Correct |
11 ms |
16000 KB |
Output is correct |
16 |
Correct |
11 ms |
16000 KB |
Output is correct |
17 |
Correct |
11 ms |
16000 KB |
Output is correct |
18 |
Correct |
11 ms |
16000 KB |
Output is correct |
19 |
Correct |
12 ms |
16000 KB |
Output is correct |
20 |
Correct |
11 ms |
16000 KB |
Output is correct |
21 |
Correct |
11 ms |
16000 KB |
Output is correct |
22 |
Correct |
12 ms |
16256 KB |
Output is correct |
23 |
Correct |
12 ms |
16128 KB |
Output is correct |
24 |
Correct |
13 ms |
16128 KB |
Output is correct |
25 |
Correct |
12 ms |
16128 KB |
Output is correct |
26 |
Correct |
12 ms |
16128 KB |
Output is correct |
27 |
Correct |
12 ms |
16128 KB |
Output is correct |
28 |
Correct |
12 ms |
16128 KB |
Output is correct |
29 |
Correct |
12 ms |
16384 KB |
Output is correct |
30 |
Correct |
13 ms |
16256 KB |
Output is correct |
31 |
Correct |
13 ms |
16128 KB |
Output is correct |
32 |
Correct |
12 ms |
16128 KB |
Output is correct |
33 |
Correct |
12 ms |
16128 KB |
Output is correct |
34 |
Correct |
12 ms |
16128 KB |
Output is correct |
35 |
Correct |
12 ms |
16128 KB |
Output is correct |
36 |
Correct |
12 ms |
16128 KB |
Output is correct |
37 |
Correct |
13 ms |
16128 KB |
Output is correct |
38 |
Correct |
12 ms |
16128 KB |
Output is correct |
39 |
Correct |
13 ms |
16256 KB |
Output is correct |
40 |
Correct |
12 ms |
16256 KB |
Output is correct |
41 |
Correct |
12 ms |
16256 KB |
Output is correct |
42 |
Correct |
12 ms |
16128 KB |
Output is correct |
43 |
Correct |
12 ms |
16128 KB |
Output is correct |
44 |
Correct |
13 ms |
16256 KB |
Output is correct |
45 |
Correct |
13 ms |
16128 KB |
Output is correct |
46 |
Correct |
12 ms |
16256 KB |
Output is correct |
47 |
Correct |
12 ms |
16128 KB |
Output is correct |
48 |
Correct |
13 ms |
16256 KB |
Output is correct |
49 |
Correct |
12 ms |
16256 KB |
Output is correct |
50 |
Correct |
12 ms |
16256 KB |
Output is correct |
51 |
Correct |
12 ms |
16256 KB |
Output is correct |
52 |
Correct |
14 ms |
16128 KB |
Output is correct |
53 |
Correct |
12 ms |
16128 KB |
Output is correct |
54 |
Correct |
13 ms |
16256 KB |
Output is correct |
55 |
Correct |
13 ms |
16128 KB |
Output is correct |
56 |
Correct |
12 ms |
16128 KB |
Output is correct |
57 |
Correct |
13 ms |
16384 KB |
Output is correct |
58 |
Correct |
12 ms |
16160 KB |
Output is correct |
59 |
Correct |
15 ms |
16128 KB |
Output is correct |
60 |
Correct |
12 ms |
16256 KB |
Output is correct |
61 |
Correct |
12 ms |
16128 KB |
Output is correct |
62 |
Correct |
12 ms |
16256 KB |
Output is correct |
63 |
Correct |
12 ms |
16128 KB |
Output is correct |
64 |
Correct |
347 ms |
32824 KB |
Output is correct |
65 |
Correct |
344 ms |
32824 KB |
Output is correct |
66 |
Correct |
362 ms |
32836 KB |
Output is correct |
67 |
Correct |
363 ms |
32696 KB |
Output is correct |
68 |
Correct |
341 ms |
30372 KB |
Output is correct |
69 |
Correct |
322 ms |
27336 KB |
Output is correct |
70 |
Correct |
284 ms |
25124 KB |
Output is correct |
71 |
Correct |
278 ms |
24184 KB |
Output is correct |
72 |
Correct |
273 ms |
23544 KB |
Output is correct |
73 |
Correct |
413 ms |
50616 KB |
Output is correct |
74 |
Correct |
419 ms |
50360 KB |
Output is correct |
75 |
Correct |
356 ms |
32696 KB |
Output is correct |
76 |
Correct |
337 ms |
30096 KB |
Output is correct |
77 |
Correct |
351 ms |
31924 KB |
Output is correct |
78 |
Correct |
351 ms |
30776 KB |
Output is correct |
79 |
Correct |
340 ms |
28992 KB |
Output is correct |
80 |
Correct |
307 ms |
25784 KB |
Output is correct |
81 |
Correct |
322 ms |
31480 KB |
Output is correct |
82 |
Correct |
350 ms |
30012 KB |
Output is correct |
83 |
Correct |
393 ms |
39996 KB |
Output is correct |
84 |
Correct |
387 ms |
41032 KB |
Output is correct |
85 |
Correct |
376 ms |
39632 KB |
Output is correct |
86 |
Correct |
350 ms |
32184 KB |
Output is correct |
87 |
Correct |
316 ms |
27680 KB |
Output is correct |
88 |
Correct |
375 ms |
34164 KB |
Output is correct |
89 |
Correct |
356 ms |
33736 KB |
Output is correct |
90 |
Correct |
343 ms |
32796 KB |
Output is correct |
91 |
Correct |
332 ms |
31956 KB |
Output is correct |
92 |
Correct |
312 ms |
31144 KB |
Output is correct |
93 |
Correct |
378 ms |
38312 KB |
Output is correct |
94 |
Correct |
360 ms |
40688 KB |
Output is correct |
95 |
Correct |
352 ms |
38768 KB |
Output is correct |
96 |
Correct |
386 ms |
36152 KB |
Output is correct |
97 |
Correct |
334 ms |
34052 KB |
Output is correct |
98 |
Correct |
343 ms |
33528 KB |
Output is correct |
99 |
Correct |
340 ms |
33204 KB |
Output is correct |
100 |
Correct |
385 ms |
37056 KB |
Output is correct |
101 |
Correct |
371 ms |
34688 KB |
Output is correct |
102 |
Correct |
348 ms |
33536 KB |
Output is correct |
103 |
Correct |
336 ms |
33112 KB |
Output is correct |
104 |
Correct |
161 ms |
24296 KB |
Output is correct |
105 |
Correct |
363 ms |
46036 KB |
Output is correct |
106 |
Correct |
311 ms |
33464 KB |
Output is correct |
107 |
Correct |
290 ms |
33588 KB |
Output is correct |
108 |
Correct |
427 ms |
51640 KB |
Output is correct |
109 |
Correct |
173 ms |
24296 KB |
Output is correct |
110 |
Correct |
380 ms |
51896 KB |
Output is correct |
111 |
Correct |
357 ms |
32648 KB |
Output is correct |