Loopy transit
Time limit2sMemory limit256 MB
Count distinct directed simple cycles in a graph of up to 9 stations, treating rotations of a loop as one.
- Level
Medium6 of 10
- Topics
- Graph, Backtracking, Brute force
- Solved
- No attempts yet
Problem
Luke likes riding public transit in the cities he visits, just for fun. What he enjoys most is finding loops: he leaves one station, travels along the connections through at least one other station, and comes back to the station he started from. He wants to know how many such loops each transit system has.
What Luke counts is simple loops. A simple loop is a sequence of distinct stations such that there is a direct connection from to for every with , and a direct connection from to as well. A loop can be written down starting from any of its stations, so every cyclic shift of such a sequence is the same simple loop. Two simple loops that visit the same set of stations in a different order are counted as different loops.
Write a program that counts the distinct simple loops in the transit system.
Input
The first line contains the number of stations in the transit system (). Stations are numbered to .
The second line contains the number of connections (). Each of the next lines describes one connection as two integers and (, , ), meaning there is a one-way connection from station to station .
Output
Print the number of distinct simple loops in the transit system.