CCO '26 P5 - Tree Traversals
View as PDFCanadian Computing Olympiad 2026: Day 2, Problem 2
Yevin Kang has a tree with vertices that are labelled with integers
from
to
. A tree is an undirected connected graph that does not
contain a cycle.
Let be a positive integer. We define
as follows.
For any two vertices , let
denote the
number of edges on the simple path connecting vertex
and vertex
.
In particular,
for all
.
A permutation of
is good if all of the
following conditions are satisfied.
for all
.
for all pairs of integers
with
.
Then, is the number of good permutations.
Yevin thinks this problem is too easy, so he gives you positive
integers
. He asks you to print the values of
, mod
.
It may also be useful to note that corresponds to the
%
operator in most programming languages, indicating the remainder after
division. For example, and
.
Input Specification
Each test has multiple test cases.
The first line of the test contains one integer
(
) — the number of test cases.
The first line of each test case contains two space-separated integers
(
).
Each of the next lines contains two space-separated integers
— indicating that there is an edge connecting
and
in the
tree. It is guaranteed that the
edges form a tree.
The next line contains integers,
— denoting the
queries.
It is guaranteed that the sum of over all test cases in a test
(denoted by
) does not exceed
.
The following table shows how the 25 available marks are distributed:
| Marks Awarded | Bounds on |
Bounds on |
Bounds on |
|---|---|---|---|
| 2 marks | |||
| 3 marks | |||
| 5 marks | |||
| 7 marks | |||
| 8 marks |
Output Specification
For each test case, output one line with space-separated integers —
the values of
, mod
.
Sample Input
2
3 3
1 2
1 3
1 2 3
6 3
1 2
1 3
3 4
3 5
3 6
1 2 3
Sample Output
0 2 2
0 6 12
Explanation for Sample Output
The two trees in the sample input are shown below.
In the first test case, for or
, both
and
are good permutations.
is not a good permutation
for all values of
because
violates the second condition.
It can be shown that no permutation is good for .
In the second test case, is a good permutation for
but not a good permutation for
because
.
Comments