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
You are given a tree consisting of (n) vertices. Each vertex (i) is painted with a color (c_i). For each distinct color (c) present in the tree, let (m_c) be the total number of vertices of color (c). You are also given an array (k) of length (n), where (k_c) ((1 \le k_c \le m_c)) represents the target component size for color (c). For every color (c) independently, your task is to choose a connected subgraph (a component) consisting of exactly (k_c) vertices. The vertices you choose for the component do not necessarily have to be of color (c). The cost of a chosen component is the sum of the shortest distances from every vertex of color (c) to the chosen component. (The distance from a vertex (v) to a component (S) is defined as the minimum number of edges on a simple path from (v) to any vertex (u) in (S)). For each color (c) from (1) to (n), find the minimum possible cost of a valid component of size (k_c). If there are no vertices of color (c) in the tree, output (-1) for that color. Each test contains multiple test cases. The first line contains the number of test cases (t) ((1 \le t \le 10^4)). The description of the test cases follows. The first line of each test case contains a single integer (n) ((1 \le n \le 2 \cdot 10^5)) — the number of vertices in the tree. The second line contains (n) integers (c_1, c_2, \ldots, c_n) ((1 \le c_i \le n)) — the colors of the vertices. The third line contains (n) integers (k_1, k_2, \ldots, k_n) ((1 \le k_i \le n)) — the target component sizes for each color. It is guaranteed that if color (c) appears (m_c \gt 0) times in the tree, then (1 \le k_c \le m_c). Each of the next (n - 1) lines contains two integers (u) and (v) ((1 \le u, v \le n)), representing an edge between vertices (u) and (v). It is guaranteed that the given edges form |
Tutorials
Submissions
Submission Id |
Author(s) |
Index |
Submitted |
Verdict |
Language |
Test Set |
Tests Passed |
Time taken (ms) |
Memory Consumed (bytes) |
Tags |
Rating |
|
385968171 |
sronak |
F |
Aug. 6, 2026, 4:31 p.m. |
OK |
C# 13 |
TESTS |
29 |
2296 |
103628800 |
|
|
|
385966945 |
FeddieLieu |
F |
Aug. 6, 2026, 4:28 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
29 |
375 |
56729600 |
|
|
|
385995212 |
Vladimir_Lenin |
F |
Aug. 6, 2026, 8:11 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
29 |
500 |
46796800 |
|
|
|
386023461 |
JeffreyLee |
F |
Aug. 7, 2026, 5:53 a.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
29 |
531 |
70963200 |
|
|
|
385965251 |
Student9128 |
F |
Aug. 6, 2026, 4:24 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
29 |
546 |
44544000 |
|
|
|
385968535 |
SpringWait |
F |
Aug. 6, 2026, 4:32 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
29 |
562 |
34508800 |
|
|
|
385979939 |
Arkcy47 |
F |
Aug. 6, 2026, 5:47 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
29 |
578 |
55500800 |
|
|
|
385977034 |
Gmt.FFF |
F |
Aug. 6, 2026, 5:26 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
29 |
593 |
40243200 |
|
|
|
385967190 |
bloo12 |
F |
Aug. 6, 2026, 4:29 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
29 |
609 |
32972800 |
|
|
|
385964431 |
therealdoug0986 |
F |
Aug. 6, 2026, 4:22 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
29 |
609 |
33894400 |
|
|
|
385992522 |
Hvmit_05 |
F |
Aug. 6, 2026, 7:36 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
29 |
609 |
65945600 |
|
|
|
385964026 |
noya2 |
F |
Aug. 6, 2026, 4:22 p.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
29 |
437 |
20992000 |
|
|
|
385995196 |
vixxa |
F |
Aug. 6, 2026, 8:10 p.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
29 |
437 |
63078400 |
|
|
|
385963651 |
Manav_Sharma1812 |
F |
Aug. 6, 2026, 4:21 p.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
29 |
484 |
64819200 |
|
|
|
386006345 |
ezraft |
F |
Aug. 7, 2026, 1:05 a.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
29 |
515 |
75571200 |
|
|
|
385976019 |
ZPAYAUR |
F |
Aug. 6, 2026, 5:19 p.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
29 |
531 |
60825600 |
|
|
|
385965546 |
Axataris |
F |
Aug. 6, 2026, 4:25 p.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
29 |
546 |
103628800 |
|
|
|
386008372 |
Dark__Seith |
F |
Aug. 7, 2026, 1:49 a.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
29 |
546 |
112128000 |
|
|
|
385967704 |
Dictator_ |
F |
Aug. 6, 2026, 4:30 p.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
29 |
562 |
70246400 |
|
|
|
386001191 |
Jensen123 |
F |
Aug. 6, 2026, 10:14 p.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
29 |
578 |
98816000 |
|
|
|
385960117 |
sahajoydeep467 |
F |
Aug. 6, 2026, 4:13 p.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
29 |
593 |
74240000 |
|
|
|
386018192 |
hawk.chan |
F |
Aug. 7, 2026, 4:40 a.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
29 |
406 |
54476800 |
|
|
|
385997398 |
cyan17 |
F |
Aug. 6, 2026, 8:45 p.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
29 |
406 |
87961600 |
|
|
|
385974648 |
415411 |
F |
Aug. 6, 2026, 5:10 p.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
29 |
406 |
138752000 |
|
|
|
386018227 |
hawk.chan |
F |
Aug. 7, 2026, 4:40 a.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
29 |
421 |
48332800 |
|
|
|
386014315 |
permutation |
F |
Aug. 7, 2026, 3:33 a.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
29 |
421 |
91852800 |
|
|
|
385974812 |
415411 |
F |
Aug. 6, 2026, 5:11 p.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
29 |
421 |
138752000 |
|
|
|
386009009 |
TaDok |
F |
Aug. 7, 2026, 2:01 a.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
29 |
437 |
73625600 |
|
|
|
385973248 |
Wangy |
F |
Aug. 6, 2026, 5:01 p.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
29 |
437 |
74956800 |
|
|
|
385965490 |
StevenKnight |
F |
Aug. 6, 2026, 4:25 p.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
29 |
453 |
40243200 |
|
|
|
385964387 |
2747 |
F |
Aug. 6, 2026, 4:22 p.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
29 |
468 |
41779200 |
|
|
|
385962765 |
EmberSeed_OFFICAL |
F |
Aug. 6, 2026, 4:19 p.m. |
OK |
GNU C11 |
TESTS |
29 |
1234 |
34611200 |
|
|
|
385969445 |
sohamgugale170 |
F |
Aug. 6, 2026, 4:33 p.m. |
OK |
GNU C11 |
TESTS |
29 |
1250 |
46899200 |
|
|
|
385961627 |
adarshshukla8 |
F |
Aug. 6, 2026, 4:16 p.m. |
OK |
Java 21 |
TESTS |
29 |
1187 |
158310400 |
|
|
|
385964454 |
Jatin_Malik |
F |
Aug. 6, 2026, 4:23 p.m. |
OK |
Java 21 |
TESTS |
29 |
1546 |
182681600 |
|
|
|
385966587 |
Ngune4ka |
F |
Aug. 6, 2026, 4:27 p.m. |
OK |
PyPy 3-64 |
TESTS |
29 |
1312 |
99532800 |
|
|
|
385966344 |
som__ |
F |
Aug. 6, 2026, 4:27 p.m. |
OK |
PyPy 3-64 |
TESTS |
29 |
1578 |
226304000 |
|
|
|
385967232 |
kabirmaske |
F |
Aug. 6, 2026, 4:29 p.m. |
OK |
PyPy 3-64 |
TESTS |
29 |
1718 |
219136000 |
|
|
|
385956896 |
chhillarankit74 |
F |
Aug. 6, 2026, 4:05 p.m. |
OK |
PyPy 3-64 |
TESTS |
29 |
1843 |
194457600 |
|
|
|
385966141 |
Smarth_coder |
F |
Aug. 6, 2026, 4:26 p.m. |
OK |
PyPy 3-64 |
TESTS |
29 |
2796 |
225894400 |
|
|
|
385969797 |
nastikys |
F |
Aug. 6, 2026, 4:34 p.m. |
OK |
PyPy 3-64 |
TESTS |
29 |
3000 |
261427200 |
|
|
|
385958050 |
de1uxegg |
F |
Aug. 6, 2026, 4:08 p.m. |
OK |
Python 3 |
TESTS |
29 |
1796 |
176537600 |
|
|
|
385964981 |
Egor |
F |
Aug. 6, 2026, 4:24 p.m. |
OK |
Rust 2024 |
TESTS |
29 |
328 |
113971200 |
|
|
remove filters
Back to search problems