Journey
Time limit1sMemory limit128 MB
Count length-d walks in a small (n<=20) graph that visit each of the first k<=7 cities at least once, modulo 1e9+9.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Graph, Bit manipulation, Matrix
- Solved
- No attempts yet
Problem
In Byteland there are cities numbered from to . These cities are connected by a network of bidirectional roads, and each pair of cities is joined by at most one road.
Byteman especially enjoys the cities numbered from to , so on every journey he visits each of these cities at least once.
A journey is a sequence of cities in which every two consecutive cities are joined by a road. A journey may start and end at any city. Compute the number of distinct journeys Byteman can make; two journeys are different if their sequences of cities differ. Because this number can be large, output it modulo .
Input
The first line contains four integers , , and (, , ), separated by single spaces. Each of the following lines describes one road with two integers , (, ): the numbers of the two cities that road connects.
Output
Output a single integer: the number of distinct journeys, taken modulo .
Hint

In the first example the roads are 1-2, 2-3, 3-1 and 2-4, and Byteman must visit cities and . The valid journeys of length are:
- 1 → 2 → 1
- 1 → 2 → 3
- 1 → 2 → 4
- 1 → 3 → 2
- 2 → 1 → 2
- 2 → 1 → 3
- 2 → 3 → 1
- 3 → 1 → 2
- 3 → 2 → 1
- 4 → 2 → 1