Poj 2057 the lost house

Source: Internet
Author: User
Tags float number uppercase character
The lost house
Time limit:3000 Ms   Memory limit:30000 K
Total submissions:2140   Accepted:886

Description

One day a snil climbed up to a big tree and finally came to the end of a branch. What a different feeling to look down from such a high place he had never been to before! However, he was very tired due to the long time of climbing, and fell asleep. an unbelievable thing happened when he woke up ---- he found himself lying in a meadow and his house originally on his back disappeared! Immediately he realized that he fell off the branch when he was sleeping! He was sure that his house must still be on the branch he had been sleeping on. The snail began to climb the tree again, since he cocould not live without his house.

When reaching the first fork of the tree, he sadly found that he cocould not remember the route that he climbed before. in order to find his lovely house, the snail decided to go to the end of every branch. it was dangerous to walk without the protection of the house, so he wished to search the tree in the best way.

Fortunately, there lived into warm-hearted worms in the tree that cocould accurately tell the snail il whether he had ever passed their places or not before he fell off.

Now our job is to help the snail Il. we pay most of our attention to two parts of the tree ---- the forks of the branches and the ends of the branches, which we call them key points because key events always happen there, such as choosing a path, getting the help from a worm and arriving at the house he is searching.

Assume all worms live at key points, and all the branches between two neighboring key points have the same distance of 1. The snail Il is now at the first fork of the tree.

Our purpose is to find a proper route along which he can find his house as soon as possible, through the analysis of the structure of the tree and the locations of the worms. the only restriction on the route is that he must not go down from a fork until he has reached all the ends grown from this fork.

The House may be left at the end of any branches in an equal probability. we focus on the mathematical expectation of the distance the snil has to cover before arriving his house. we wish the value to be as small as possible.

As your strated in figure-1, the snail Il is at the key point 1 and his house is at a certain point among 2, 4 and 5. A worm lives at point 3, who can tell the snail il whether his house is at one of Point 4 and 5 or not. therefore, the snail Il can choose two strategies. he can go to point 2 first. if he cannot find the house there, He shocould go back to point 1, and then reaches point 4 (or 5) by point 3. if still not, he has to return point 3, then go to point 5 (or 4), where he will undoubtedly find his house. in this choice, the snail covers distances of 1, 4, 6 corresponding to the circumstances under which the House is located at point 2, 4 (or 5), 5 (or 4) respectively. so the expectation value is (1 + 4 + 6)/3 = 11/3. obviusly, this strategy does not make full use of the information from the worm. if the snail il goes to point 3 and gets useful information from the worm first, and then chooses to go back to point 1 then towards point 2, or go to point 4 or 5 to take his chance, the distances he covers will be 2, 3, 4 corresponding to the different locations of the house. in such a strategy, the mathematical expectation will be (2 + 3 + 4)/3 = 3, and it is the very route along which the snil shoshould search the tree.

Input

The input contains several sets of test data. each set begins with a line containing one integer N, no more than 1000, which indicates the number of key points in the tree. then follow n lines describing the n key points. for convenience, we number all the key points from 1 to n. the key point numbered with 1 is always the first fork of the tree. other numbers may be any key points in the tree should t the first fork. the I-th line in these n lines describes the key point with number I. each line consists of one integer and one uppercase character 'y' or 'N' separated by a single space, which represents the number of the previous key point and whether there lives a worm ('y' means lives and 'n' means not ). the previous key point means the neighboring key point in the shortest path between this key point and the key point numbered 1. in the above authentication, the previous key point of point 2 or 3 is point 1, while the previous key point of Point 4 or 5 is point 3. this integer is-1 for the Key Point 1, means it has no previous key point. you can assume a fork has at most eight branches. the first set in the sample input describes the above workflow.

A test case of N = 0 indicates the end of input, and shoshould not be processed.

Output

Output one line for each set of input data. The line contains one float number with exactly four digits after the decimal point, which is the mathematical expectation value.

Sample Input

5-1 N1 N1 Y3 N3 N10-1 N1 Y1 N2 N2 N2 N3 N3 Y8 N8 N6-1 N1 N1 Y1 N3 N3 N0

Sample output

3.00005.00003.5000

Source

Beijing 2004

A very classic tree-like DP. After thinking for a long time, my mind was too messy, and I had to worry about the sequence in which the subnode was selected. It turned out to be greedy. Other ideas are also messy. I can't help but read the problem report. After reading the report, I woke up instantly, saving myself a lot of misunderstandings.
Le [N]: Number of leaf nodes for access nodes
Fa [x]: number of steps to be taken when the root node x cannot be accessed successfully
Su [x]: number of steps required to successfully access each leaf node and
The final result is Su [1]/le [1].
Relational Expression: Fa [x] + = Fa [y] + 2; 2 is X to Y.
Su [x] + = (Fa [x] + 1) * le [y] + su [y]; X in Fa [x] is not the number of steps that cannot be successfully accessed by the full root node X, but is constantly updated. before accessing y, many of the Brothers of Y are certainly accessed, failed to access these brothers, and then access y.
Greedy sorting:
Bool CMP (INT P1, int P2)
{
Return (Fa [P1] + 2) * le [P2] <(Fa [P2] + 2) * le [P1];
}
Access P1 first and then P2, and access P1 first. Which of the following is smaller? The relationship can be obtained after simplification.
In fact, if you really don't want to sort it like this, you can use binary DP, which should also be possible.
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cstdlib>#include <cmath>#define N 1100using namespace std;struct num{    int y,next;}a[N*2];int b[N];bool status[N],ch[N];char str[10];int su[N],fa[N],le[N],Top;bool cmp(int p1,int p2){    return (fa[p1]+2)*le[p2]<(fa[p2]+2)*le[p1];}int main(){    //freopen("data.txt","r",stdin);    void addeage(int x,int y);    void dfs(int u);    int n;    while(scanf("%d",&n)!=EOF)    {        if(n==0)        {            break;        }        Top = 0;        memset(b,-1,sizeof(b));        memset(status,false,sizeof(status));        for(int i=1;i<=n;i++)        {            int x;            scanf("%d %s",&x,str);            if(x!=-1&&i!=x)            {                addeage(i,x);                addeage(x,i);            }            if(str[0]=='Y')            {                status[i] = true;            }        }        memset(ch,false,sizeof(ch));        memset(fa,0,sizeof(fa));        memset(le,0,sizeof(le));        memset(su,0,sizeof(su));        dfs(1);        double ans = (double)su[1]/(double)le[1];        printf("%.4lf\n",ans);    }    return 0;}void addeage(int x,int y){    a[Top].y = y;    a[Top].next = b[x];    b[x] = Top++;}void dfs(int u){    ch[u] = true;    bool c = true;    for(int i=b[u];i!=-1;i=a[i].next)    {        int y = a[i].y;        if(!ch[y])        {            c = false;            break;        }    }    if(c)    {        fa[u] = 0;        le[u] = 1;        su[u] = 0;        return ;    }    int w[N],sum=0;    for(int i=b[u];i!=-1;i=a[i].next)    {        int y = a[i].y;        if(!ch[y])        {            dfs(y);            w[sum++] = y;            le[u]+=le[y];        }    }    sort(w,w+sum,cmp);    for(int i=0;i<=sum-1;i++)    {        int y = w[i];        su[u]+=(fa[u]+1)*le[y]+su[y];        fa[u]+=(fa[y]+2);    }    if(status[u])    {        fa[u] = 0;    }}



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.