Super-Fast Circular Races
Time limit1sMemory limit128 MB
Given a directed graph where each vertex has out-degree and in-degree at most two, count the ways to cover every vertex with vertex-disjoint simple directed cycles, modulo 10000, or report NIE if impossible.
- Level
Medium5 of 10
- Topics
- Graph, DFS, Combinatorics, Implementation
- Solved
- No attempts yet
Problem
A racing club wants to break the record for the total number of tracks (segments) used across several circular races held at the same time. The city has intersections. Tracks connect intersections; every track is one-way, so it may be driven in one fixed direction only.
The club will hold one or more races. Each race runs around a single closed loop of tracks (a directed cycle), going round and round. For safety, no two races may share a track or an intersection, and no race may pass through the same intersection twice. In other words, the chosen races form a set of vertex-disjoint and edge-disjoint simple directed cycles.
The club wants the total number of tracks used by all races to equal exactly (the previous record was ). A single simple directed cycle uses as many tracks as the intersections it visits, so a total of tracks means every intersection is visited by exactly one race.
Each intersection has at most two outgoing tracks and at most two incoming tracks. No track starts and ends at the same intersection.
Decide whether such an arrangement of races (with total length exactly ) exists, and if so, count the number of different arrangements. Print the uppercase word NIE if it is impossible; otherwise print the number of arrangements modulo .
Input
The first line contains two integers and separated by a space (, ). Here is the number of intersections and the target total track length, and is the number of tracks. Each of the next lines contains two integers and separated by a space, describing a one-way track that leaves intersection and ends at intersection .
Output
If no arrangement of races with total length exists, print the single word NIE. Otherwise print one integer: the number of such arrangements modulo .