POJ 2057 The lost house

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.