#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int>> adjList;
vector<int> parent;
string isBased;
vector<int> potentialBasedCount;
vector<int> dp;
int result = 0;
void depthFirstSearchA(int u, int p)
{
parent[u] = p;
potentialBasedCount[u] = (isBased[u] == '1');
for (auto &v : adjList[u])
if (v != p)
{
depthFirstSearchA(v, u);
potentialBasedCount[u] += potentialBasedCount[v];
}
}
void depthFirstSearchB(int u, int p)
{
int V = 0;
for (auto &v : adjList[u])
if (v != p)
{
depthFirstSearchB(v, u);
dp[u] += dp[v];
if (dp[v] > dp[V])
V = v;
}
if (isBased[u] == '1')
{
result = max(dp[V] + 1, result);
dp[u] = max(1, dp[u] - 1);
}
result = max(dp[u], result);
}
int main()
{
// freopen("inp.txt", "r", stdin);
ios::sync_with_stdio(false);
cin >> n;
adjList.resize(n + 1);
parent.resize(n + 1);
for (int u, v, i = 1; i < n; i++)
{
cin >> u >> v;
adjList[u].emplace_back(v);
adjList[v].emplace_back(u);
}
cin >> isBased;
isBased = "!" + isBased;
potentialBasedCount.resize(n + 1);
depthFirstSearchA(1, -1);
dp.resize(n + 1);
depthFirstSearchB(1, -1);
cout << result;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
2 ms |
460 KB |
Output is correct |
36 |
Correct |
2 ms |
412 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
468 KB |
Output is correct |
51 |
Correct |
1 ms |
468 KB |
Output is correct |
52 |
Correct |
1 ms |
468 KB |
Output is correct |
53 |
Correct |
1 ms |
468 KB |
Output is correct |
54 |
Correct |
1 ms |
468 KB |
Output is correct |
55 |
Correct |
1 ms |
468 KB |
Output is correct |
56 |
Correct |
1 ms |
428 KB |
Output is correct |
57 |
Correct |
1 ms |
468 KB |
Output is correct |
58 |
Correct |
1 ms |
468 KB |
Output is correct |
59 |
Correct |
1 ms |
464 KB |
Output is correct |
60 |
Correct |
1 ms |
468 KB |
Output is correct |
61 |
Correct |
1 ms |
468 KB |
Output is correct |
62 |
Correct |
1 ms |
492 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
2 ms |
460 KB |
Output is correct |
36 |
Correct |
2 ms |
412 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
468 KB |
Output is correct |
51 |
Correct |
1 ms |
468 KB |
Output is correct |
52 |
Correct |
1 ms |
468 KB |
Output is correct |
53 |
Correct |
1 ms |
468 KB |
Output is correct |
54 |
Correct |
1 ms |
468 KB |
Output is correct |
55 |
Correct |
1 ms |
468 KB |
Output is correct |
56 |
Correct |
1 ms |
428 KB |
Output is correct |
57 |
Correct |
1 ms |
468 KB |
Output is correct |
58 |
Correct |
1 ms |
468 KB |
Output is correct |
59 |
Correct |
1 ms |
464 KB |
Output is correct |
60 |
Correct |
1 ms |
468 KB |
Output is correct |
61 |
Correct |
1 ms |
468 KB |
Output is correct |
62 |
Correct |
1 ms |
492 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
95 ms |
15128 KB |
Output is correct |
65 |
Correct |
123 ms |
15124 KB |
Output is correct |
66 |
Correct |
156 ms |
15152 KB |
Output is correct |
67 |
Correct |
134 ms |
15176 KB |
Output is correct |
68 |
Correct |
122 ms |
15188 KB |
Output is correct |
69 |
Correct |
91 ms |
15176 KB |
Output is correct |
70 |
Correct |
120 ms |
15196 KB |
Output is correct |
71 |
Correct |
153 ms |
15188 KB |
Output is correct |
72 |
Correct |
92 ms |
15188 KB |
Output is correct |
73 |
Correct |
128 ms |
27940 KB |
Output is correct |
74 |
Correct |
154 ms |
27772 KB |
Output is correct |
75 |
Correct |
100 ms |
15048 KB |
Output is correct |
76 |
Correct |
94 ms |
15100 KB |
Output is correct |
77 |
Correct |
106 ms |
15092 KB |
Output is correct |
78 |
Correct |
106 ms |
15012 KB |
Output is correct |
79 |
Correct |
126 ms |
15060 KB |
Output is correct |
80 |
Correct |
118 ms |
15072 KB |
Output is correct |
81 |
Correct |
88 ms |
15596 KB |
Output is correct |
82 |
Correct |
95 ms |
15212 KB |
Output is correct |
83 |
Correct |
109 ms |
20240 KB |
Output is correct |
84 |
Correct |
114 ms |
22360 KB |
Output is correct |
85 |
Correct |
110 ms |
22016 KB |
Output is correct |
86 |
Correct |
99 ms |
16440 KB |
Output is correct |
87 |
Correct |
90 ms |
15872 KB |
Output is correct |
88 |
Correct |
94 ms |
16420 KB |
Output is correct |
89 |
Correct |
100 ms |
16364 KB |
Output is correct |
90 |
Correct |
117 ms |
16240 KB |
Output is correct |
91 |
Correct |
102 ms |
16352 KB |
Output is correct |
92 |
Correct |
126 ms |
16808 KB |
Output is correct |
93 |
Correct |
92 ms |
19172 KB |
Output is correct |
94 |
Correct |
109 ms |
20940 KB |
Output is correct |
95 |
Correct |
96 ms |
19588 KB |
Output is correct |
96 |
Correct |
96 ms |
17680 KB |
Output is correct |
97 |
Correct |
123 ms |
16216 KB |
Output is correct |
98 |
Correct |
112 ms |
15852 KB |
Output is correct |
99 |
Correct |
95 ms |
15532 KB |
Output is correct |
100 |
Correct |
92 ms |
18696 KB |
Output is correct |
101 |
Correct |
93 ms |
16668 KB |
Output is correct |
102 |
Correct |
88 ms |
15928 KB |
Output is correct |
103 |
Correct |
102 ms |
15732 KB |
Output is correct |
104 |
Correct |
60 ms |
15952 KB |
Output is correct |
105 |
Correct |
142 ms |
24644 KB |
Output is correct |
106 |
Correct |
71 ms |
16092 KB |
Output is correct |
107 |
Correct |
61 ms |
15932 KB |
Output is correct |
108 |
Correct |
135 ms |
28508 KB |
Output is correct |
109 |
Correct |
87 ms |
15916 KB |
Output is correct |
110 |
Correct |
118 ms |
28868 KB |
Output is correct |
111 |
Correct |
104 ms |
15052 KB |
Output is correct |