Irreversible Reactions
Time limit2sMemory limit512 MB
Given a directed graph with random transitions, find the expected time to leave the set of states that can still return to the start, or report infinity.
- Level
Hard8 of 10
- Topics
- Graph, Probability, Dynamic programming, Math
- Solved
- No attempts yet
Problem
Life is ephemeral. What is done sometimes cannot be undone. In this respect, chemical reaction networks resemble life.
For brevity, we model a chemical reaction network as follows. There are states in a chemical reaction network. The reaction starts at state . distinct pairs (, ) of states have a relation such that can be transitioned to . For each unit of time, a state is transitioned to other states if there is at least one state that can be transitioned from it. If there are no such states, the state does not change. If there are two or more states to which the current state can be transitioned, the next state is selected uniformly at random from these states.
In an experiment on the chemical reaction network, we stop the experiment when the current state does not change. In addition, after transitions, it might reach a state from which it is impossible to return to by repeating transitions. In such cases we also stop the experiment. Your task as a bioinformatics researcher is to write a program to compute the expected time until we stop the experiment. If the expected time is infinite, output instead.
A more precise description of the experiment termination criteria is the following. If there are no states that can be transitioned from the current state, we stop the experiment immediately. Otherwise, the experiment does not stop if it is possible to return to from the current state by repeating zero or more transitions. In other words, the state itself is always considered as a state that can return to in this case.
Input
The input consists of a single test case in the format below.
The first line contains three integers (), (), (). is the number of states in a chemical reaction network. is the number of transitions between states. is the initial state. The -th of the following lines represents the -th transition, which means a state can be transitioned to (, ). Note that it does not necessarily imply can be transitioned to . It is guaranteed that the pairs and are distinct. That is, for , either or holds.
Output
Output the expected time to reach one of the states from which it is impossible to return to the initial state . The expected time can be huge, thus output the expected time modulo . More precisely, if it is finite, the expected time can be represented as an irreducible fraction , and you should output the minimum non-negative integer which satisfies . You may assume that such exists for the given input.
If the expected time is infinite, output instead.