On a tree, each of M families picks one of the other N-1 cities uniformly and independently; find the expected number of roads used by every family.
Medium7TreeProbabilityDFSNo attempts yetTime limit2sMemory limit512 MBA country has N cities and N−1 roads. The cities are numbered 0 through N−1. Every road is bidirectional and joins two different cities.
A simple path is a sequence of two or more cities in which no city appears twice and consecutive cities are joined by a road. Every pair of cities in this country is joined by a simple path, so the road network is a tree.
M families live in the country, numbered 0 through M−1. Each family lives in one city, and several families may live in the same city.
For the holidays every family picked one city to travel to. The chosen city differs from the city the family lives in, and each of the other N−1 cities is equally likely. The families choose independently. During the holidays each family travels along the simple path from the city it lives in to the city it chose.
Depending on what the families picked, some roads are travelled by every family. Let L be the number of such roads. Write a program that computes the expected value of L.
The first line contains the number of cities N and the number of families M. (2≤N≤51, 1≤M≤50)
Each of the next N−1 lines contains the numbers of the two cities joined by a road. The given roads always form a tree.
The last line contains M numbers, the city each family lives in.
Print the expected value of L on the first line, rounded to nine digits after the decimal point. Always print all nine digits. If the expected value is exactly 1.5, print 1.500000000.
The input is chosen so that the expected value never sits on a rounding boundary at the ninth decimal digit, so double precision arithmetic produces the same output.
Suppose three cities are joined in the order 0, 1, 2, and a single family lives in city 0. With probability 21 the family picks city 1 and travels one road, and with probability 21 it picks city 2 and travels two roads. There is only one family, so the roads it travels are exactly the roads travelled by every family. The expected value is 21×1+21×2=1.5.
In the same country with two families that both live in city 0, both pick city 2 with probability 41, and then L=2. In every other case L=1, so the expected value is 41×2+43×1=1.25.