Trees and Primes
Time limit2sMemory limit512 MB
Given a tree on N vertices, find the probability that a uniformly random pair of distinct vertices has a prime distance.
- Level
Medium7 of 10
- Topics
- Tree, DFS, Number theory, Divide and conquer
- Solved
- No attempts yet
Problem
You are given a tree with vertices, numbered 1 through . The distance between two vertices is the number of edges on the path that joins them.
Two distinct vertices are chosen uniformly at random, so each of the unordered pairs is equally likely. Write a program that computes the probability that the distance between the two chosen vertices is a prime number.
Input
The first line contains the number of vertices ().
Each of the next lines contains the numbers and of two vertices joined by an edge, separated by a space (, ).
The given graph is always a tree.
Output
Print the probability that the distance between the two chosen vertices is prime, with exactly 10 digits after the decimal point. Round half up at the eleventh digit and keep the trailing zeros. For example, if the probability is exactly , print 0.5000000000.