School Tour
InterviewTime limit1sMemory limit256 MB
Choose a spanning tree of the graph rooted at the entrance that contains the fixed edge to building 1, find the min and max possible number of uphill edges among its edges, and print (max)^2 - (min)^2.
- Level
Medium6 of 10
- Topics
- Minimum spanning tree, Union-find, Greedy, Graph
- Solved
- No attempts yet
Problem
Gukhee guides high school students around the campus during the summer vacation. The campus is on a hillside, so a road between two buildings is either an uphill road or a downhill road. Gukhee surveyed every road once, taking the entrance as the starting point.
The buildings are numbered through , and the entrance is numbered . Exactly one road leaves the entrance, and it leads to building .
To introduce every building, Gukhee has to choose a set of roads that connects the entrance and all buildings, and she chooses as few roads as possible. There are places in total, so she chooses exactly roads, and those roads alone must let her reach every building from the entrance.
The fatigue of a choice is , where is the number of uphill roads among the chosen roads. Uphill and downhill come from the original survey only, so walking down a downhill road and climbing back up it later is not counted. The road between the entrance and building belongs to every choice, so it counts as well.
Among all valid choices, one has the largest fatigue and one has the smallest. Find the difference between the two.
Input
The first line has the number of buildings () and the number of roads between buildings ().
The second line describes the road between the entrance and building in the form . Here is always and is always .
Each of the next lines has , meaning that a road joins building and building (, ). is for an uphill road and for a downhill road.
No pair of buildings is joined by more than one road. Every building is reachable from the entrance.
Output
Print the difference between the largest fatigue and the smallest fatigue on one line.