CCO '26 P3 - Beyond Counting
View as PDFCanadian Computing Olympiad 2026: Day 1, Problem 3
Andy Jiang is studying data structures. One day, his friend Austin Zhu gave him a task on trees.
Austin provided a tree with vertices, numbered from
to
. Each
vertex
has a value
.
For each query, Austin asked Andy to consider a path between two
vertices and
, and compute how many times a given value
appears on that path.
Andy glanced at the problem and thought that this task was too easy for him.
Instead of just counting occurrences, Andy decided to challenge himself
further. For each query, he wants to know how the frequency of
compares to other values on the same path.
Formally, for each query :
Consider the simple path from
to
.
Let
be the number of occurrences of value
on this path.
Andy defines the rank of as
That is, one plus the number of distinct values that appear more
frequently than on the path. Note that it is possible the value
does not appear on the path, i.e.
. In this
case, you should return 1 plus the number of distinct values on the
path.
In some test cases, the queries are given in an encoded form as described below.
Help Andy compute the rank of for each query.
Input Specification
The first line contains three positive integers ,
, and
.
The second line contains integers
.
The next lines each contain two integers
, representing the
-th edge.
Each of the next lines contains three integers
,
describing the
-th query.
Let . For each query
, the
actual parameters are defined as:
After computing the answer to the -th query, set
It may also be
useful to note that
corresponds to the
% operator in most
programming languages, indicating the remainder after division. For
example, and
.
The following table shows how the 25 available marks are distributed:
| Marks Awarded | Bounds on |
Bounds on |
Additional Constraints |
|---|---|---|---|
| 1 mark | None. | ||
| 1 mark | All | ||
| 4 marks | |||
| 4 marks | |||
| 5 marks | |||
| 3 marks | None. | ||
| 7 marks |
Output Specification
For each query, output the answer to the query on a new line.
Sample Input 1
5 5 0
1 2 3 4 4
4 3
2 5
1 3
3 2
4 5 3
4 5 4
4 5 5
1 5 1
1 5 4
Sample Output 1
2
1
4
1
1
Sample Input 2
5 5 1
1 2 3 4 4
4 3
2 5
1 3
3 2
4 5 3
2 3 2
3 4 4
2 1 999999997
5 4 3
Sample Output 2
2
1
4
1
1
Comments