Solutions are presented as using the least memory and the fastest execution time. It also takes the top
10 most recent solutions from each language. If you want to limit to a specific index, click the
"Solved" button and go to that problem.
Problems
B'You are given a tree (an undirected connected acyclic graph) which initially only contains vertex 1 . There will be several queries to the given tree. In the i -th query, vertex i + 1 will appear and be connected to vertex p_i ( 1 <= p_i <= i ). After each query, please find out the least number of operations required to make the current tree has two centroids. In one operation, you can add one vertex and one edge to the tree such that it remains a tree. A vertex is called a centroid if its removal splits the tree into subtrees with at most lfloor frac{n}{2} rfloor vertices each, with n as the number of vertices of the tree. For example, the centroid of the following tree is 3 because the biggest subtree after removing the centroid has 2 vertices. In the next tree, vertex 1 and 2 are both centroids. Each test contains multiple test cases. The first line contains the number of test cases t ( 1 <= t <= 10^4 ). The description of the test cases follows. The first line of each test case contains a single integer n ( 2 <= n <= 5 cdot 10^{5} ) -- the number of nodes of the final tree. The second line of each test case contains n - 1 integers p_1, p_2, ldots, p_{n - 1} ( 1 <= p_i <= i ) -- the index of the vertex that is connected to vertex i + 1 . It is guaranteed that the sum of n over all test cases does not exceed 5 cdot 10^{5} . For each test case, output n - 1 integers. The i -th integer is the answer to the i -th query -- the least number of operations required to make the current tree have two centroids. We can show that an answer always exists. The illustrations below are of the fourth example test case. After the third query: After the fourth query: After the fifth query: After the sixth query: '... |
Tutorials
Submissions
Submission Id |
Author(s) |
Index |
Submitted |
Verdict |
Language |
Test Set |
Tests Passed |
Time taken (ms) |
Memory Consumed (bytes) |
Tags |
Rating |
206288404 |
DitaMirika |
D |
May 18, 2023, 1:23 a.m. |
OK |
GNU C++14 |
TESTS |
95 |
296 |
73932800 |
|
2800 |
205976228 |
realFZzzz |
D |
May 15, 2023, 11:50 a.m. |
OK |
GNU C++14 |
TESTS |
95 |
311 |
45056000 |
|
2800 |
206288418 |
DitaMirika |
D |
May 18, 2023, 1:23 a.m. |
OK |
GNU C++14 |
TESTS |
95 |
311 |
73932800 |
|
2800 |
206074162 |
zltzlt |
D |
May 16, 2023, 10:33 a.m. |
OK |
GNU C++14 |
TESTS |
95 |
358 |
73523200 |
|
2800 |
206074138 |
zltzlt |
D |
May 16, 2023, 10:32 a.m. |
OK |
GNU C++14 |
TESTS |
95 |
358 |
73523200 |
|
2800 |
205961088 |
HorizonWind |
D |
May 15, 2023, 9:12 a.m. |
OK |
GNU C++14 |
TESTS |
95 |
358 |
79667200 |
|
2800 |
205905662 |
arthur.nascimento |
D |
May 14, 2023, 5:31 p.m. |
OK |
GNU C++14 |
TESTS |
93 |
373 |
159027200 |
|
2800 |
205894399 |
AlternatingCurrent |
D |
May 14, 2023, 4:10 p.m. |
OK |
GNU C++14 |
TESTS |
93 |
389 |
68812800 |
|
2800 |
206344405 |
mdshakib2003. |
D |
May 18, 2023, 1:18 p.m. |
OK |
GNU C++14 |
TESTS |
95 |
389 |
87244800 |
|
2800 |
206166256 |
wenyay |
D |
May 17, 2023, 2:42 a.m. |
OK |
GNU C++14 |
TESTS |
95 |
389 |
87244800 |
|
2800 |
206346917 |
successzjl23 |
D |
May 18, 2023, 1:41 p.m. |
OK |
GNU C++17 |
TESTS |
95 |
202 |
63488000 |
|
2800 |
206179083 |
__finn |
D |
May 17, 2023, 7:13 a.m. |
OK |
GNU C++17 |
TESTS |
95 |
265 |
39321600 |
|
2800 |
205878863 |
Um_nik |
D |
May 14, 2023, 3:25 p.m. |
OK |
GNU C++17 |
TESTS |
93 |
295 |
37888000 |
|
2800 |
206078505 |
TsunamiNoLetGo |
D |
May 16, 2023, 11:16 a.m. |
OK |
GNU C++17 |
TESTS |
95 |
296 |
43417600 |
|
2800 |
206364912 |
klu_2100031552 |
D |
May 18, 2023, 4:27 p.m. |
OK |
GNU C++17 |
TESTS |
95 |
311 |
89088000 |
|
2800 |
206151039 |
Sedmoklasnikut |
D |
May 16, 2023, 7:39 p.m. |
OK |
GNU C++17 |
TESTS |
95 |
311 |
89088000 |
|
2800 |
205950958 |
lbntxdy |
D |
May 15, 2023, 7:12 a.m. |
OK |
GNU C++17 |
TESTS |
95 |
327 |
70451200 |
|
2800 |
206151073 |
Sedmoklasnikut |
D |
May 16, 2023, 7:39 p.m. |
OK |
GNU C++17 |
TESTS |
95 |
327 |
89088000 |
|
2800 |
206029288 |
Rafi22 |
D |
May 15, 2023, 8:52 p.m. |
OK |
GNU C++17 |
TESTS |
95 |
358 |
45568000 |
|
2800 |
205994813 |
yuexia |
D |
May 15, 2023, 2:13 p.m. |
OK |
GNU C++17 |
TESTS |
95 |
358 |
50380800 |
|
2800 |
206344650 |
ExplodingKonjac |
D |
May 18, 2023, 1:20 p.m. |
OK |
GNU C++17 (64) |
TESTS |
95 |
202 |
84684800 |
|
2800 |
206344447 |
ExplodingKonjac |
D |
May 18, 2023, 1:19 p.m. |
OK |
GNU C++17 (64) |
TESTS |
95 |
218 |
84684800 |
|
2800 |
206156135 |
lukameladze1 |
D |
May 16, 2023, 9:08 p.m. |
OK |
GNU C++17 (64) |
TESTS |
95 |
233 |
68300800 |
|
2800 |
205922017 |
rainboy |
D |
May 14, 2023, 8:41 p.m. |
OK |
GNU C++17 (64) |
TESTS |
95 |
265 |
89600000 |
|
2800 |
205915565 |
YeongTree |
D |
May 14, 2023, 7:15 p.m. |
OK |
GNU C++17 (64) |
TESTS |
94 |
296 |
78745600 |
|
2800 |
205873414 |
q-w-q-w-q |
D |
May 14, 2023, 3:13 p.m. |
OK |
GNU C++17 (64) |
TESTS |
93 |
296 |
98713600 |
|
2800 |
205905690 |
Xellos |
D |
May 14, 2023, 5:32 p.m. |
OK |
GNU C++17 (64) |
TESTS |
93 |
311 |
93798400 |
|
2800 |
205909870 |
Sana |
D |
May 14, 2023, 6:23 p.m. |
OK |
GNU C++17 (64) |
TESTS |
93 |
311 |
101683200 |
|
2800 |
206044362 |
Xylenox |
D |
May 16, 2023, 4:09 a.m. |
OK |
GNU C++17 (64) |
TESTS |
95 |
311 |
111001600 |
|
2800 |
205939985 |
Alex_Wei |
D |
May 15, 2023, 4:25 a.m. |
OK |
GNU C++17 (64) |
TESTS |
95 |
327 |
106598400 |
|
2800 |
205963414 |
maspy |
D |
May 15, 2023, 9:37 a.m. |
OK |
GNU C++20 (64) |
TESTS |
95 |
170 |
95436800 |
|
2800 |
206153404 |
Boboge |
D |
May 16, 2023, 8:16 p.m. |
OK |
GNU C++20 (64) |
TESTS |
95 |
187 |
44134400 |
|
2800 |
205997992 |
Thallium54 |
D |
May 15, 2023, 2:43 p.m. |
OK |
GNU C++20 (64) |
TESTS |
95 |
187 |
44134400 |
|
2800 |
206169723 |
arodnap33 |
D |
May 17, 2023, 4:09 a.m. |
OK |
GNU C++20 (64) |
TESTS |
95 |
202 |
48128000 |
|
2800 |
206151739 |
arodnap33 |
D |
May 16, 2023, 7:50 p.m. |
OK |
GNU C++20 (64) |
TESTS |
95 |
202 |
48128000 |
|
2800 |
205877520 |
tourist |
D |
May 14, 2023, 3:22 p.m. |
OK |
GNU C++20 (64) |
TESTS |
93 |
202 |
48128000 |
|
2800 |
205884073 |
maroonrk |
D |
May 14, 2023, 3:38 p.m. |
OK |
GNU C++20 (64) |
TESTS |
93 |
202 |
91648000 |
|
2800 |
206009575 |
testmayank123 |
D |
May 15, 2023, 4:38 p.m. |
OK |
GNU C++20 (64) |
TESTS |
95 |
218 |
68812800 |
|
2800 |
205884864 |
Endagorion |
D |
May 14, 2023, 3:40 p.m. |
OK |
GNU C++20 (64) |
TESTS |
93 |
218 |
68812800 |
|
2800 |
205886393 |
PyIsBestLang |
D |
May 14, 2023, 3:44 p.m. |
OK |
GNU C++20 (64) |
TESTS |
93 |
233 |
44953600 |
|
2800 |
205893258 |
chinerist |
D |
May 14, 2023, 4:06 p.m. |
OK |
PyPy 3-64 |
TESTS |
93 |
935 |
155033600 |
|
2800 |
remove filters
Back to search problems