Interconnect
Time limit1sMemory limit128 MB
Given an initial graph on up to 30 towns, compute the exact expected number of random-edge additions needed until the graph becomes fully connected, as a reduced fraction.
- Level
Hard8 of 10
- Topics
- Union-find, Math, Probability, Dynamic programming
- Solved
- No attempts yet
Problem
In the Kingdom of Lipshire there are towns. Some of the old roads have fallen into such disrepair that they can no longer be used, so the kingdom has decided to rebuild its road network.
The new road network must interconnect every town: there must be a path between any two towns.
The road administration can build exactly one road per year, but the builders are completely out of control. No matter what orders they are given, each year they choose two distinct towns and uniformly at random among all unordered pairs and build a road between them — even if those towns are already directly or indirectly connected. Every pair is equally likely. A traveler can enter or leave each road only at its two endpoint towns, and every road is bidirectional.
Given the towns that are still joined by the good roads that remain, determine the expected number of years until the whole road network becomes interconnected.
Input
The first line contains two integers and (, ) — the number of towns and the number of good roads that still remain. Towns are numbered from to .
Each of the next lines contains two integers and (, ) describing a remaining road between towns and . There may be several roads between the same pair of towns, but no road connects a town to itself.
Output
The expected number of years is always a rational number. Output it as an exact reduced fraction in the form p/q, where , , and the fraction equals the expected number of years. If the road network is already interconnected, the expected value is and must be printed as 0/1.
For example, an expected value of is printed as 3/2, and an expected value of is printed as 1/1.