#D12757. Fractal Tree
Fractal Tree
Fractal Tree
problem
AOR Ika likes rooted trees with a fractal (self-similar) structure. Consider using the weighted rooted tree consisting of vertices to represent the rooted tree with the following fractal structure.
- is the addition of a tree rooted at and having a tree structure similar to (same cost) for each vertex of .
- The root of is the same as that of .
The tree represented in this way is, for example, as shown in the figure below.
AOR Ika is trying to do a depth-first search for , but finds that it takes a lot of time to trace all the vertices. Therefore, we decided to skip some node visits by performing a depth-first search with a policy of transitioning with a probability of and not transitioning with a probability of at the time of transition during a depth-first search. Given and the probability , find the expected value of the sum of the costs of all edges to follow when performing a depth-first search for . The information of is given by the number of vertices and the information of the edges of , and the vertex is the root. Each vertex is labeled , and the th edge costs the vertices and It is tied with. The non-deterministic algorithm of the depth-first search that transitions to a child with a probability of , which is dealt with in this problem, is expressed as follows. The sum of the costs of the edges that the output follows.
- Prepare an empty stack .
- Set
- Push the root vertex of to .
- Extract the first element of and make it .
- For each child of , do the following with probability and do nothing with probability .
- Add vertex to . Then add the weight of the edge connecting to to .
- If S is not empty, transition to 3.
- Output .
output
Print the answer in one line. If the relative or absolute error is less than , then AC. Also, output a line break at the end.
Example
Input
0.75 4 1 2 1 2 3 3 3 4 10
Output
24.8569335938
inputFormat
outputFormat
output follows.
- Prepare an empty stack .
- Set
- Push the root vertex of to .
- Extract the first element of and make it .
- For each child of , do the following with probability and do nothing with probability .
- Add vertex to . Then add the weight of the edge connecting to to .
- If S is not empty, transition to 3.
- Output .
output
Print the answer in one line. If the relative or absolute error is less than , then AC. Also, output a line break at the end.
Example
Input
0.75 4 1 2 1 2 3 3 3 4 10
Output
24.8569335938
样例
0.75
4
1 2 1
2 3 3
3 4 10
24.8569335938