Duathlon
Time limit1sMemory limit1024 MB
Count ordered triples (s, c, f) of distinct vertices such that some simple path visits s, then c, then f, in an undirected graph with n up to 1e5.
- Level
Hard8 of 10
- Topics
- Graph, BFS, DFS, Dynamic programming
- Solved
- No attempts yet
Problem
The street network of Byteburg has intersections and two-way street segments, and each segment joins two intersections. Byteburg was chosen to host the upcoming duathlon championship. The competition has two legs: a running leg, followed by a cycling leg.
The route is built like this. First pick three distinct intersections , and as the start, change and finish stations. Then build a route that starts at , goes through and ends at . For safety, the route visits each intersection at most once.
Before planning the route, the mayor wants to count the triples for which such a route exists. Compute that number.
Input
The first line contains the number of intersections and the number of streets (, ). Each of the next lines describes one street with the numbers and of the two intersections it joins (, ). At most one street joins a given pair of intersections.
Output
Print the number of triples for which the route can be built.
Hint
In the first sample the 8 triples are , , , , , , , .
In the second sample the 14 triples are , , , , , , , , , , , , , .