CCO '26 P6 - Walking on a Graph
View as PDFCanadian Computing Olympiad 2026: Day 2, Problem 3
There is a graph with nodes, numbered from 1 to
. Each node is
coloured either black or white. Additionally, it is known that node 1 is
black and node 2 is white. For any
and
where
, there
exists a directed edge from node
to
that is either red or blue.
Its colour is determined using the following logic:
If
and the nodes have the same colour, then it is red.
If
and the nodes have different colours, then it is blue.
If
and the nodes have the same colour, then it is blue.
If
and the nodes have different colours, then it is red.
LoBren's favourite colour is initially blue. He then takes a walk on the graph (note that walks allow for repeated vertices and edges). He uses the following rules when walking:
If he is currently on node 1, his favourite colour becomes blue.
Otherwise, if he is currently on node 2, his favourite colour becomes red.
He then traverses an outgoing edge from his current node with the same colour as his favourite colour. It can be shown that such an edge must exist.
Finally, he optionally repeats the process.
By writing down the nodes he visits, in order, he gets a list
. Find the number of possible lists, mod
, that satisfy the following conditions:
The list starts at node 1 and ends at node 2.
For all
where
, node
appears at most once in the list.
For all
where
, we have
.
It is provable that the number of such lists is finite.
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
The first line of input contains a single integer, .
The next line contains a string of length , consisting of the
characters
B and W. If the character is
,
then node
is black. Otherwise, it is white. It is guaranteed that
node 1 is black and node 2 is white.
The following table shows how the 25 available marks are distributed:
| Marks Awarded | Bounds on | Additional Constraints |
|---|---|---|
| 1 mark | None. | |
| 3 marks | None. | |
| 4 marks | There exists exactly one black node. | |
| 4 marks | There exists an integer | |
| 6 marks | There exist at most 5 black nodes. | |
| 7 marks | None. |
Output Specification
On a single line, output the number of possible lists, modulo .
Sample Input 1
4
BWWB
Sample Output 1
4
Explanation for Sample Output 1
The graph looks like:
The solid lines represent blue edges, while the dashed lines represent red edges. The possible paths are:
The favourite colour is red at the underlined nodes, and blue otherwise.
Sample Input 2
12
BWBWBBBWWBBW
Output for Sample Input 2
3377552
Comments