2057The Lost House
大神的指导,加上大神的题解文档,我自己照着他的代码打了一遍,通过了
详细看看网站http://blog.sina.com.cn/s/blog_5f5353cc0100hd08.html
我的ACCEPT代码,想通了就感觉个之前做过的一道聚会的树形dp有点像
#include "iostream" #include "algorithm" #include "string.h" #include <iomanip> #define M 1015 using namespace std; int tree[M][M],leave[M],success[M],fail[M]; char h[M]; int cmp(int u,int v) { return (fail[u]+2)*leave[v]<(fail[v]+2)*leave[u]; } void work(int a){ int i,j,tem[M]; if(tree[a][0]==0){ leave[a]=1; success[a]=0; fail[a]=0; } for(i=1;i<=tree[a][0];i++){ work(tree[a][i]); } for(i=1;i<=tree[a][0];i++){ if(h[a]=='N')fail[a]+=fail[tree[a][i]]+2; leave[a]+=leave[tree[a][i]]; } for(i=1;i<=tree[a][0];i++){ tem[i]=tree[a][i]; } sort(tem+1,tem+tree[a][0]+1,cmp); for(i=1,j=0;i<=tree[a][0];i++){ success[a]+=(j+1)*leave[tem[i]]+success[tem[i]]; j+=fail[tem[i]]+2; } } int main(){ char b; int a,n,i; while(cin>>n&&n){ memset(tree,0,sizeof(tree)); memset(success,0,sizeof(success)); memset(fail,0,sizeof(fail)); memset(leave,0,sizeof(leave)); for(i=1;i<=n;i++){ cin>>a>>b; if(a!=-1){ tree[a][0]++; tree[a][tree[a][0]]=i; } h[i]=b; } work(1); double tem=(double)success[1]/(double)leave[1]; cout <<setiosflags(ios::fixed); cout<< setprecision(4)<<tem<<endl; } }