Sublime Marathon
Time limit2sMemory limit512 MB
Given a tree with N intersections, count the ways to add exactly two roads so that some two intersections are joined by three internally disjoint paths.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Combinatorics
- Solved
- No attempts yet
Problem
The Sublime Alliance picked a suitable site to host an algorithm marathon. The site has intersections and two-way roads, and exactly one path connects every pair of distinct intersections. (In other words, the site is a tree.)
An algorithm marathon should give runners many routes they can trace with shortest-path algorithms in their heads. So choose two different intersections, one as the start and one as the end. The goal is for Soongsil University, Korea University, and Hanyang University to each get a route from the start to the end, with the three routes sharing no intersection other than the start and the end.

For method 1: with 2 as the start and 3 as the end, three routes that share no intersections are formed.
For method 2: with 1 as the start and 2 as the end, three routes that share no intersections are formed.
For method 3: with 2 as the start and 4 as the end, three routes that share no intersections are formed.
So the tree has 3 possible methods, and the answer is 3.
You must add exactly two roads to the site so that the goal can be reached. Count the ways to choose which two roads to add. The count can be large, so print it modulo . After the roads are added, two intersections may be joined by at most one road, and no road may start and end at the same intersection. (In other words, the graph must be simple.)
Input
The first line contains the number of intersections ().
Each of the next lines contains two intersection numbers and , separated by a space, which are connected by a road. ()
The input is guaranteed to be a tree.
Output
Print the number of ways to choose two roads to add so that the goal can be reached, modulo .