#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 200'005;
vector<int> adjlist[MAXN];
string str;
int ans = 0;
int dfs(int x, int par) {
int take = 0, notake = 0;
if (str[x] == '1') {
notake--;
for (int nxt : adjlist[x]) {
if (nxt == par) continue;
int val = dfs(nxt, x);
take = max(take, val);
notake += val;
}
ans = max(ans, max(notake, take + 1));
return max(notake, 1);
}
else {
for (int nxt : adjlist[x]) {
if (nxt == par) continue;
int val = dfs(nxt, x);
notake += val;
}
ans = max(ans, notake);
return notake;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int nodes; cin >> nodes;
for (int i = 0; i < nodes - 1; i++) {
int a, b; cin >> a >> b;
adjlist[a].push_back(b);
adjlist[b].push_back(a);
}
cin >> str;
str = ' ' + str;
dfs(1, -1);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
5020 KB |
Output is correct |
3 |
Correct |
2 ms |
5020 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5016 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5020 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
3 ms |
5020 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5024 KB |
Output is correct |
21 |
Correct |
3 ms |
5020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
5020 KB |
Output is correct |
3 |
Correct |
2 ms |
5020 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5016 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5020 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
3 ms |
5020 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5024 KB |
Output is correct |
21 |
Correct |
3 ms |
5020 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5060 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5024 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5080 KB |
Output is correct |
31 |
Correct |
3 ms |
5032 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5032 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5064 KB |
Output is correct |
37 |
Correct |
3 ms |
5036 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
4 ms |
5028 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
4 ms |
5076 KB |
Output is correct |
43 |
Correct |
5 ms |
5204 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5072 KB |
Output is correct |
46 |
Correct |
3 ms |
5036 KB |
Output is correct |
47 |
Correct |
3 ms |
5032 KB |
Output is correct |
48 |
Correct |
4 ms |
5076 KB |
Output is correct |
49 |
Correct |
3 ms |
5080 KB |
Output is correct |
50 |
Correct |
4 ms |
5032 KB |
Output is correct |
51 |
Correct |
3 ms |
5076 KB |
Output is correct |
52 |
Correct |
4 ms |
5032 KB |
Output is correct |
53 |
Correct |
3 ms |
5076 KB |
Output is correct |
54 |
Correct |
3 ms |
5076 KB |
Output is correct |
55 |
Correct |
3 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
4 ms |
5204 KB |
Output is correct |
58 |
Correct |
3 ms |
5076 KB |
Output is correct |
59 |
Correct |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5160 KB |
Output is correct |
61 |
Correct |
3 ms |
5076 KB |
Output is correct |
62 |
Correct |
3 ms |
5160 KB |
Output is correct |
63 |
Correct |
4 ms |
5008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
5020 KB |
Output is correct |
3 |
Correct |
2 ms |
5020 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5016 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5020 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
3 ms |
5020 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5024 KB |
Output is correct |
21 |
Correct |
3 ms |
5020 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5060 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5024 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5080 KB |
Output is correct |
31 |
Correct |
3 ms |
5032 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5032 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5064 KB |
Output is correct |
37 |
Correct |
3 ms |
5036 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
4 ms |
5028 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
4 ms |
5076 KB |
Output is correct |
43 |
Correct |
5 ms |
5204 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5072 KB |
Output is correct |
46 |
Correct |
3 ms |
5036 KB |
Output is correct |
47 |
Correct |
3 ms |
5032 KB |
Output is correct |
48 |
Correct |
4 ms |
5076 KB |
Output is correct |
49 |
Correct |
3 ms |
5080 KB |
Output is correct |
50 |
Correct |
4 ms |
5032 KB |
Output is correct |
51 |
Correct |
3 ms |
5076 KB |
Output is correct |
52 |
Correct |
4 ms |
5032 KB |
Output is correct |
53 |
Correct |
3 ms |
5076 KB |
Output is correct |
54 |
Correct |
3 ms |
5076 KB |
Output is correct |
55 |
Correct |
3 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
4 ms |
5204 KB |
Output is correct |
58 |
Correct |
3 ms |
5076 KB |
Output is correct |
59 |
Correct |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5160 KB |
Output is correct |
61 |
Correct |
3 ms |
5076 KB |
Output is correct |
62 |
Correct |
3 ms |
5160 KB |
Output is correct |
63 |
Correct |
4 ms |
5008 KB |
Output is correct |
64 |
Correct |
99 ms |
14516 KB |
Output is correct |
65 |
Correct |
72 ms |
14524 KB |
Output is correct |
66 |
Correct |
103 ms |
14556 KB |
Output is correct |
67 |
Correct |
80 ms |
14604 KB |
Output is correct |
68 |
Correct |
101 ms |
14552 KB |
Output is correct |
69 |
Correct |
94 ms |
14508 KB |
Output is correct |
70 |
Correct |
69 ms |
14548 KB |
Output is correct |
71 |
Correct |
108 ms |
14592 KB |
Output is correct |
72 |
Correct |
101 ms |
14520 KB |
Output is correct |
73 |
Correct |
123 ms |
24428 KB |
Output is correct |
74 |
Correct |
77 ms |
24324 KB |
Output is correct |
75 |
Correct |
101 ms |
14488 KB |
Output is correct |
76 |
Correct |
111 ms |
14456 KB |
Output is correct |
77 |
Correct |
81 ms |
14472 KB |
Output is correct |
78 |
Correct |
61 ms |
14476 KB |
Output is correct |
79 |
Correct |
59 ms |
14464 KB |
Output is correct |
80 |
Correct |
94 ms |
14440 KB |
Output is correct |
81 |
Correct |
78 ms |
14700 KB |
Output is correct |
82 |
Correct |
83 ms |
14624 KB |
Output is correct |
83 |
Correct |
70 ms |
18392 KB |
Output is correct |
84 |
Correct |
125 ms |
20092 KB |
Output is correct |
85 |
Correct |
87 ms |
19916 KB |
Output is correct |
86 |
Correct |
94 ms |
15388 KB |
Output is correct |
87 |
Correct |
61 ms |
14964 KB |
Output is correct |
88 |
Correct |
61 ms |
15396 KB |
Output is correct |
89 |
Correct |
71 ms |
15448 KB |
Output is correct |
90 |
Correct |
67 ms |
15272 KB |
Output is correct |
91 |
Correct |
76 ms |
15512 KB |
Output is correct |
92 |
Correct |
62 ms |
15752 KB |
Output is correct |
93 |
Correct |
59 ms |
17560 KB |
Output is correct |
94 |
Correct |
96 ms |
19028 KB |
Output is correct |
95 |
Correct |
83 ms |
17888 KB |
Output is correct |
96 |
Correct |
69 ms |
16488 KB |
Output is correct |
97 |
Correct |
85 ms |
15292 KB |
Output is correct |
98 |
Correct |
81 ms |
15072 KB |
Output is correct |
99 |
Correct |
67 ms |
14884 KB |
Output is correct |
100 |
Correct |
72 ms |
17328 KB |
Output is correct |
101 |
Correct |
64 ms |
15684 KB |
Output is correct |
102 |
Correct |
57 ms |
15068 KB |
Output is correct |
103 |
Correct |
97 ms |
15056 KB |
Output is correct |
104 |
Correct |
46 ms |
15280 KB |
Output is correct |
105 |
Correct |
73 ms |
21844 KB |
Output is correct |
106 |
Correct |
44 ms |
15164 KB |
Output is correct |
107 |
Correct |
59 ms |
15160 KB |
Output is correct |
108 |
Correct |
73 ms |
25068 KB |
Output is correct |
109 |
Correct |
73 ms |
15256 KB |
Output is correct |
110 |
Correct |
118 ms |
25292 KB |
Output is correct |
111 |
Correct |
97 ms |
14464 KB |
Output is correct |