Given a tree on N vertices, find the probability that a uniformly random pair of distinct vertices has a prime distance.
Medium7TreeDFSNumber theoryDivide and conquerNo attempts yetTime limit2sMemory limit512 MB
Description
Examples5
Problem
You are given a tree with N vertices, numbered 1 through N. 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 (2N) 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 N (2≤N≤50000).
Each of the next N−1 lines contains the numbers u and v of two vertices joined by an edge, separated by a space (1≤u,v≤N, u=v).
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 21, print 0.5000000000.