RandomGraph
SRM 620 · 2013-12-22 · by cgy4ever
Problem Statement
You are given the
Notes
- Your return value must have an absolute or relative error less than 1e-9.
- A connected component is a maximal set S of vertices such that you can get from any vertex in S to any other vertex in S by following a sequence of edges. For example, if a graph with n=5 contains edges 0-2, 2-4, and 1-3, its connected components are {0,2,4} and {1,3}.
Constraints
- n will be between 2 and 50, inclusive.
- p will be between 0 and 1000, inclusive.
7 0 Returns: 0.0
The probability of each edge is 0. Therefore, this graph will always have 7 isolated vertices = 7 connected components, each with just a single vertex.
3 620 Returns: 0.0
This graph only has 3 vertices, so it is impossible to have a connected component with at least 4 vertices.
4 500 Returns: 0.59375
There are 64 different graphs on 4 labeled vertices. As p=500, each of these 64 graphs is equally likely to be generated by our procedure. A graph on 4 vertices has a connected component with 4 or more vertices if and only if the entire graph is connected. Out of our 64 possible graphs, 38 are connected. Therefore, the probability we are looking for is 38/64.
8 100 Returns: 0.33566851611343496
In this case, some of the good graphs have two connected components, each with 4 vertices.
15 50 Returns: 0.5686761670525845
Submissions are judged against all 90 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class RandomGraph with a public method double probability(int n, int p) · 90 test cases · 2 s / 256 MB per case