This question is expected.
First, when you see the expectation, you will think of dividing the problem into several subproblems and calculating the expectation separately. Therefore, you can use dynamic planning for this question.
Note that the probability of each leaf having a house is equal. The answer is to traverse the minimum number of steps per leaf/The total number of leaves.
Therefore, the problem is to find the minimum number of steps to traverse all leaves.
We make fail [x] A Child tree rooted in x unable to find the minimum number of steps to the house.
Su [x] is the minimum number of steps for finding a house in a subtree rooted in x.
Le [x] indicates the number of leaves in the subtree rooted in x.
Fail [x] = sigma (fail [y] + 2); (worm [x] = false) fail [x] = 0 (worm [x] = true );
Su [x] + = (fail [x] + 1) * le [y] + su [y]; (fail [x] indicates the number of steps where no house was found before y)
Obviously, the order of su [x] and son x is closely related.
The first idea is to enumerate all the full arrays. Although each node has a maximum of eight sons! = 40320, too large.
The second idea is greed. We can use the adjusted idea to determine the order of the son.
Two Adjacent sons whose y1 and y2 are set to x. If y1 is earlier than y2, ans1 = (fail [x] + 1) * le [y1] + su [y1] + (fail [x] + 2 + fail [y1] + 1) * le [y2] + su [y2]
If y1, y2 is exchanged, ans2 = (fail [x] + 1) * le [y2] + su [y2] + (fail [x] + 2 + fail [y2] + 1) * le [y1] + su [y1]
Then the ans1-ans2 = (fail [y1] + 2) * le [y2]-(fail [y2] + 2)-le [y1].
So you can sort the data according to this order. Then calculate.
[Summary]
Please be sure to find out what the requirements are, do not blindly get started, and try your best to simplify the problem. Like the above practice, the entire process does not involve any floating point operations, which is very good.
[Code]
[Cpp]
# Include <iostream>
# Include <cstring>
# Include <string>
# Include <cstdio>
# Include <algorithm>
# Include <vector>
Using namespace std;
Const int n= 1005;
Int su [N], fail [N], le [N], n, root;
Bool w [N];
Vector <int> a [N];
Bool cmp (int x, int y)
{
Return (fail [x] + 2) * le [y] <(fail [y] + 2) * le [x];
}
Void dp (int x)
{
Int I, y;
If (a [x]. empty ())
{
Le [x] = 1;
Return;
}
For (I = 0; I <a [x]. size (); I ++)
{
Y = a [x] [I];
Dp (y );
Le [x] + = le [y];
}
Sort (a [x]. begin (), a [x]. end (), cmp );
For (I = 0; I <a [x]. size (); I ++)
{
Y = a [x] [I];
Su [x] + = (fail [x] + 1) * le [y] + su [y];
Fail [x] + = fail [y] + 2;
}
If (w [x]) fail [x] = 0;
}
Int main ()
{
Int I, j;
Char ch;
Freopen ("in", "r", stdin );
While (1)
{
Scanf ("% d", & n );
If (n = 0) break;
Memset (fail, 0, sizeof (fail ));
Memset (su, 0, sizeof (su ));
Memset (le, 0, sizeof (le ));
Memset (w, 0, sizeof (w ));
For (I = 1; I <= n; I ++)
A [I]. clear ();
For (I = 1; I <= n; I ++)
{
Scanf ("% d % c", & j, & ch );
If (j =-1) root = I;
Else a [j]. push_back (I );
W [I] = (ch = 'y '? True: false );
}
Dp (root );
Printf ("%. 4f \ n", 1.0 * su [root]/le [root]);
}
}
Author: ascii991