#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
const int MAXN = 2e5 + 25;
vector <int> adj[MAXN];
int n, dp[MAXN][2];
bool ok[MAXN];
void dfs (int pos, int par) {
dp[pos][1] = ok[pos];
int sum = 0, mx = 0, mx2 = 0;
for (auto j : adj[pos]) {
if (j == par) continue;
dfs(j, pos);
sum += dp[j][1];
mx = max(mx, dp[j][1]);
mx2 = max(mx2, dp[j][0]);
}
dp[pos][1] = max(dp[pos][1], sum - ok[pos]);
dp[pos][0] = max({sum - ok[pos], ok[pos] + mx, mx2});
}
int main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
char c; cin >> c; ok[i] = c == '1';
}
dfs(1, -1);
cout << dp[1][0] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6740 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6740 KB |
Output is correct |
18 |
Correct |
2 ms |
6736 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6740 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6740 KB |
Output is correct |
18 |
Correct |
2 ms |
6736 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6780 KB |
Output is correct |
24 |
Correct |
2 ms |
6744 KB |
Output is correct |
25 |
Correct |
3 ms |
6748 KB |
Output is correct |
26 |
Correct |
3 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
3 ms |
6748 KB |
Output is correct |
29 |
Correct |
3 ms |
6744 KB |
Output is correct |
30 |
Correct |
2 ms |
6748 KB |
Output is correct |
31 |
Correct |
2 ms |
6756 KB |
Output is correct |
32 |
Correct |
2 ms |
6748 KB |
Output is correct |
33 |
Correct |
2 ms |
6748 KB |
Output is correct |
34 |
Correct |
2 ms |
6748 KB |
Output is correct |
35 |
Correct |
2 ms |
6748 KB |
Output is correct |
36 |
Correct |
2 ms |
6816 KB |
Output is correct |
37 |
Correct |
2 ms |
6744 KB |
Output is correct |
38 |
Correct |
2 ms |
6748 KB |
Output is correct |
39 |
Correct |
2 ms |
6748 KB |
Output is correct |
40 |
Correct |
2 ms |
6748 KB |
Output is correct |
41 |
Correct |
2 ms |
6756 KB |
Output is correct |
42 |
Correct |
3 ms |
6816 KB |
Output is correct |
43 |
Correct |
2 ms |
6748 KB |
Output is correct |
44 |
Correct |
2 ms |
6744 KB |
Output is correct |
45 |
Correct |
2 ms |
6748 KB |
Output is correct |
46 |
Correct |
2 ms |
6748 KB |
Output is correct |
47 |
Correct |
2 ms |
6744 KB |
Output is correct |
48 |
Correct |
3 ms |
6756 KB |
Output is correct |
49 |
Correct |
2 ms |
6748 KB |
Output is correct |
50 |
Correct |
2 ms |
6748 KB |
Output is correct |
51 |
Correct |
3 ms |
6828 KB |
Output is correct |
52 |
Correct |
2 ms |
6748 KB |
Output is correct |
53 |
Correct |
2 ms |
6748 KB |
Output is correct |
54 |
Correct |
3 ms |
6744 KB |
Output is correct |
55 |
Correct |
2 ms |
6748 KB |
Output is correct |
56 |
Correct |
2 ms |
6748 KB |
Output is correct |
57 |
Correct |
2 ms |
6748 KB |
Output is correct |
58 |
Correct |
2 ms |
6744 KB |
Output is correct |
59 |
Correct |
2 ms |
6748 KB |
Output is correct |
60 |
Correct |
3 ms |
6748 KB |
Output is correct |
61 |
Correct |
3 ms |
6748 KB |
Output is correct |
62 |
Correct |
2 ms |
6744 KB |
Output is correct |
63 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6740 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6740 KB |
Output is correct |
18 |
Correct |
2 ms |
6736 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6780 KB |
Output is correct |
24 |
Correct |
2 ms |
6744 KB |
Output is correct |
25 |
Correct |
3 ms |
6748 KB |
Output is correct |
26 |
Correct |
3 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
3 ms |
6748 KB |
Output is correct |
29 |
Correct |
3 ms |
6744 KB |
Output is correct |
30 |
Correct |
2 ms |
6748 KB |
Output is correct |
31 |
Correct |
2 ms |
6756 KB |
Output is correct |
32 |
Correct |
2 ms |
6748 KB |
Output is correct |
33 |
Correct |
2 ms |
6748 KB |
Output is correct |
34 |
Correct |
2 ms |
6748 KB |
Output is correct |
35 |
Correct |
2 ms |
6748 KB |
Output is correct |
36 |
Correct |
2 ms |
6816 KB |
Output is correct |
37 |
Correct |
2 ms |
6744 KB |
Output is correct |
38 |
Correct |
2 ms |
6748 KB |
Output is correct |
39 |
Correct |
2 ms |
6748 KB |
Output is correct |
40 |
Correct |
2 ms |
6748 KB |
Output is correct |
41 |
Correct |
2 ms |
6756 KB |
Output is correct |
42 |
Correct |
3 ms |
6816 KB |
Output is correct |
43 |
Correct |
2 ms |
6748 KB |
Output is correct |
44 |
Correct |
2 ms |
6744 KB |
Output is correct |
45 |
Correct |
2 ms |
6748 KB |
Output is correct |
46 |
Correct |
2 ms |
6748 KB |
Output is correct |
47 |
Correct |
2 ms |
6744 KB |
Output is correct |
48 |
Correct |
3 ms |
6756 KB |
Output is correct |
49 |
Correct |
2 ms |
6748 KB |
Output is correct |
50 |
Correct |
2 ms |
6748 KB |
Output is correct |
51 |
Correct |
3 ms |
6828 KB |
Output is correct |
52 |
Correct |
2 ms |
6748 KB |
Output is correct |
53 |
Correct |
2 ms |
6748 KB |
Output is correct |
54 |
Correct |
3 ms |
6744 KB |
Output is correct |
55 |
Correct |
2 ms |
6748 KB |
Output is correct |
56 |
Correct |
2 ms |
6748 KB |
Output is correct |
57 |
Correct |
2 ms |
6748 KB |
Output is correct |
58 |
Correct |
2 ms |
6744 KB |
Output is correct |
59 |
Correct |
2 ms |
6748 KB |
Output is correct |
60 |
Correct |
3 ms |
6748 KB |
Output is correct |
61 |
Correct |
3 ms |
6748 KB |
Output is correct |
62 |
Correct |
2 ms |
6744 KB |
Output is correct |
63 |
Correct |
2 ms |
6748 KB |
Output is correct |
64 |
Correct |
70 ms |
16236 KB |
Output is correct |
65 |
Correct |
89 ms |
15952 KB |
Output is correct |
66 |
Correct |
168 ms |
15908 KB |
Output is correct |
67 |
Correct |
83 ms |
15956 KB |
Output is correct |
68 |
Correct |
106 ms |
15956 KB |
Output is correct |
69 |
Correct |
71 ms |
15868 KB |
Output is correct |
70 |
Correct |
98 ms |
15952 KB |
Output is correct |
71 |
Correct |
70 ms |
15964 KB |
Output is correct |
72 |
Correct |
66 ms |
15952 KB |
Output is correct |
73 |
Correct |
119 ms |
31132 KB |
Output is correct |
74 |
Correct |
119 ms |
30868 KB |
Output is correct |
75 |
Correct |
68 ms |
15700 KB |
Output is correct |
76 |
Correct |
72 ms |
15700 KB |
Output is correct |
77 |
Correct |
66 ms |
15892 KB |
Output is correct |
78 |
Correct |
118 ms |
15696 KB |
Output is correct |
79 |
Correct |
66 ms |
15700 KB |
Output is correct |
80 |
Correct |
79 ms |
15696 KB |
Output is correct |
81 |
Correct |
57 ms |
16076 KB |
Output is correct |
82 |
Correct |
75 ms |
15956 KB |
Output is correct |
83 |
Correct |
105 ms |
21932 KB |
Output is correct |
84 |
Correct |
80 ms |
24656 KB |
Output is correct |
85 |
Correct |
81 ms |
24144 KB |
Output is correct |
86 |
Correct |
70 ms |
17492 KB |
Output is correct |
87 |
Correct |
86 ms |
16984 KB |
Output is correct |
88 |
Correct |
77 ms |
17108 KB |
Output is correct |
89 |
Correct |
64 ms |
17232 KB |
Output is correct |
90 |
Correct |
110 ms |
17072 KB |
Output is correct |
91 |
Correct |
99 ms |
17440 KB |
Output is correct |
92 |
Correct |
66 ms |
17596 KB |
Output is correct |
93 |
Correct |
70 ms |
20636 KB |
Output is correct |
94 |
Correct |
66 ms |
22476 KB |
Output is correct |
95 |
Correct |
65 ms |
20944 KB |
Output is correct |
96 |
Correct |
70 ms |
18904 KB |
Output is correct |
97 |
Correct |
62 ms |
16980 KB |
Output is correct |
98 |
Correct |
88 ms |
16504 KB |
Output is correct |
99 |
Correct |
60 ms |
16212 KB |
Output is correct |
100 |
Correct |
80 ms |
20228 KB |
Output is correct |
101 |
Correct |
89 ms |
17736 KB |
Output is correct |
102 |
Correct |
70 ms |
16628 KB |
Output is correct |
103 |
Correct |
64 ms |
16464 KB |
Output is correct |
104 |
Correct |
44 ms |
16584 KB |
Output is correct |
105 |
Correct |
91 ms |
27472 KB |
Output is correct |
106 |
Correct |
45 ms |
16584 KB |
Output is correct |
107 |
Correct |
48 ms |
16576 KB |
Output is correct |
108 |
Correct |
87 ms |
31924 KB |
Output is correct |
109 |
Correct |
53 ms |
16576 KB |
Output is correct |
110 |
Correct |
103 ms |
32312 KB |
Output is correct |
111 |
Correct |
54 ms |
15700 KB |
Output is correct |