0% found this document useful (0 votes)
34 views3 pages

Shuffle Codeforces

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

Shuffle Codeforces

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

6/9/24, 10:16 PM Problem - E - Codeforces

 |
NguyenDucHien | Logout

HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP

PROBLEMS SUBMIT CODE MY SUBMISSIONS STATUS HACKS ROOM STANDINGS CUSTOM INVOCATION

Codeforces Global Round 26


E. Shuffle Contest is running
time limit per test: 2 seconds
02:18:26
memory limit per test: 256 megabytes
input: standard input Contestant
output: standard output

Two hungry red pandas, Oscar and Lura, have a tree T with n nodes. They are willing to
perform the following shuffle procedure on the whole tree T exactly once. With this shuffle
→ Submit?
procedure, they will create a new tree out of the nodes of the old tree.

1. Choose any node V from the original tree T . Create a new tree T2 , with V as the root. Language: Python 3.8.10
Almost always, if you send a solution
2. Remove V from T , such that the original tree is split into one or more subtrees (or zero on PyPy, it works much faster
subtrees, if V is the only node in T ). Choose
Choose File No file chosen
3. Shuffle each subtree with the same procedure (again choosing any node as the root), then file:

connect all shuffled subtrees' roots back to V to finish constructing T2 . Be careful: there is 50 points penalty for
submission which fails the pretests or
resubmission (except failure on the first test,
After this, Oscar and Lura are left with a new tree T2 . They can only eat leaves and are very denial of judgement or similar verdicts).
"Passed pretests" submission verdict doesn't
hungry, so please find the maximum number of leaves over all trees that can be created in
guarantee that the solution is absolutely
exactly one shuffle. correct and it will pass system tests.

Note that leaves are all nodes with degree 1. Thus, the root may be considered as a leaf if it Submit
has only one child.

Input → Score table


4
The first line contains a single integer t (1 ≤ t ≤ 10 ) — the number of test cases.
Score
The first line of every test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 105 ) — the number Problem A 446
of nodes within the original tree T .
Problem B 668
The next n − 1 lines each contain two integers u and v (1 ≤ u, v ≤ n) — an edge within the Problem C1 668
original tree T . The given edges form a tree.
Problem C2 1114
5 Problem D 2227
The sum of n over all test cases does not exceed 3 ⋅ 10 .
Problem E 2672
Output
Problem F 2672
For each test case, output a single integer — the maximum number of leaves achievable with
exactly one shuffle procedure on the whole tree. Problem G 3563
Problem H 4454
Example
Successful hack 100
input Copy
Unsuccessful hack -50
4
5 Unsuccessful submission -50
1 2 Resubmission -50
1 3
* If you solve problem on 00:41 from the first attempt
2 4
2 5
5
1 2
2 3
3 4
4 5
6
1 2
1 3
1 4
1 5
1 6

https://codeforces.com/contest/1984/problem/E 1/3
6/9/24, 10:16 PM Problem - E - Codeforces
10
 9 3
8 1
10 6
8 5
7 8
4 6
1 3
10 1
2 7

output Copy

4
3
5
6
Note
In the first test case, it can be shown that the maximum number of leaves is 4. To accomplish
this, we can start our shuffle with selecting node 3 as the new root.

Next, we are left only with one subtree, in which we can select node 2 to be the new root of
that subtree.

This will force all 3 remaining nodes to be leaves, and once we connect them back to our new
root, the shuffled subtree looks like this:

We connect the shuffled subtree back to our new root of our new tree. Our final tree has four
leaves (including the root), and looks like this:

https://codeforces.com/contest/1984/problem/E 2/3
6/9/24, 10:16 PM Problem - E - Codeforces

In our second test case, we have a line of five nodes. It can be shown that the maximum
number of leaves after one shuffle is 3. We can start off with node 2, which forces node 1 to
become a leaf. Then, if we select node 4 on the right side, we will also have nodes 3 and 5 as
leaves.

The third test case is a star graph with six nodes. The number of leaves cannot increase, thus
our answer will be 5 (if we start the shuffling with the original root node).

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov


The only programming contests Web 2.0 platform
Server time: Jun/09/2024 22:16:27UTC+7 (l2).
Desktop version, switch to mobile version.
Privacy Policy

Supported by

https://codeforces.com/contest/1984/problem/E 3/3

You might also like