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
Yousef is given a tree(^{\text{∗}}) of (n) vertices numbered from (1) to (n). Let (S) be the set of all leaves(^{\text{†}}) of the given tree (this set is determined from the original tree and does not change). Yousef repeats the following process until the number of unchosen vertices is at most one : Choose two distinct vertices (u, v \in S) that have not been chosen before. Add (d(u, v)) to the total cost, where (d(u, v)) is the number of edges on the simple path between (u) and (v). Mark (u) and (v) as chosen. Your task is to help Yousef determine the minimum possible total cost achievable after the process ends. (^{\text{∗}})A tree is an undirected connected graph in which there are no cycles. (^{\text{†}})A leaf of a tree is a vertex that is connected to at most one other vertex. The first line contains an integer (t) ((1 \le t \le 10^4)) — the number of test cases. Then (t) test cases follow. Each test case consists of several lines. The first line of the test case contains an integer (n) ((3 \le n \le 2 \cdot 10^5)) — the number of vertices in the tree. Then (n − 1) lines follow, each of which contains two integers (u) and (v) ((1 \le u, v \le n), (u \ne v)) that describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges. It is guaranteed that the sum of (n) over all test cases does not exceed (2 \cdot 10^5). For each test case, output a single integer — the minimum possible total cost achievable after the process ends. In the first test case, the set of leaves (S) contains (\{1, 3, 4\}). Yousef can do the following: Choose (u = 1), (v = 3), with (d(1, 3) = 2). Yousef adds (2) to the total cost and marks vertices (1) and (3) as chosen. Since there is one vertex left unchosen, the process stops, and the total cost is (2). It can be show |
Tutorials
Submissions
Submission Id |
Author(s) |
Index |
Submitted |
Verdict |
Language |
Test Set |
Tests Passed |
Time taken (ms) |
Memory Consumed (bytes) |
Tags |
Rating |
|
373154129 |
Ron47 |
H |
April 30, 2026, 4:42 p.m. |
OK |
C# 13 |
TESTS |
16 |
218 |
74342400 |
|
|
|
373155767 |
gbunuxi |
H |
April 30, 2026, 4:48 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
16 |
109 |
22630400 |
|
|
|
373150520 |
MCPlayer542 |
H |
April 30, 2026, 4:31 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
16 |
125 |
12800000 |
|
|
|
373165691 |
Darko2026 |
H |
April 30, 2026, 5:48 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
16 |
125 |
28262400 |
|
|
|
373173906 |
MD_Morshed_Jaman |
H |
April 30, 2026, 7:36 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
16 |
140 |
8908800 |
|
|
|
373185157 |
Dev_islam |
H |
May 1, 2026, 12:27 a.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
16 |
140 |
9011200 |
|
|
|
373169782 |
Linuyin |
H |
April 30, 2026, 6:36 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
16 |
140 |
16281600 |
|
|
|
373167911 |
keli25768 |
H |
April 30, 2026, 6:13 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
16 |
140 |
21811200 |
|
|
|
373155797 |
More |
H |
April 30, 2026, 4:48 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
16 |
140 |
44339200 |
|
|
|
373152443 |
David_24 |
H |
April 30, 2026, 4:37 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
16 |
140 |
72908800 |
|
|
|
373153188 |
whysoserious123123 |
H |
April 30, 2026, 4:39 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
16 |
156 |
8601600 |
|
|
remove filters
Back to search problems