poj 2057 The Lost House
来源:互联网 发布:iphone移动数据开关 编辑:程序博客网 时间:2024/05/21 06:59
论文题,树dp
多说两句,这个题的决策是在一个节点时选择子节点遍历的顺序
通过贪心来优化dp
#include<cstring>#include<cstdio>#include<vector>#include<cstring>#include<algorithm>using namespace std;const int maxn = 1123;vector<int> edge[maxn];void init(){ for(int i=0;i<maxn;i++){ edge[i].clear(); }}int suc[maxn],fai[maxn],siz[maxn];bool hav[maxn];bool cmper(int x,int y){ return (fai[x] + 2) * siz[y] < (fai[y] + 2) * siz[x];}void dfs(int st){ siz[st] = edge[st].size()==0; for(vector<int>::iterator it = edge[st].begin();it != edge[st].end();it++){ int x = *it; dfs(x); siz[st] += siz[x]; } sort(edge[st].begin(),edge[st].end(),cmper); suc[st] = fai[st] = 0; for(vector<int>::iterator it = edge[st].begin();it != edge[st].end();it++){ int x = *it; suc[st] += (fai[st]+1)*siz[x] + suc[x]; fai[st] += fai[x] + 2; } if(hav[st]) fai[st] = 0;}int main(){ int n; while(~scanf("%d",&n) && n){ char in[3]; int fa; init(); for(int i=1;i<=n;i++){ scanf("%d %s",&fa,in); if(fa != -1) edge[fa].push_back(i); hav[i] = *in == 'Y'; } dfs(1); printf("%.4f\n",suc[1]*1.0/siz[1]); } return 0;}
0 0
- POJ 2057 The lost house
- poj 2057 The Lost House
- POJ 2057 The Lost House
- POJ 2057 The Lost House
- POJ 2057 The Lost House
- poj-2057 The Lost House
- poj 2057 The Lost House
- POJ 2057 The Lost House 树状DP
- 【树形DP】【poj 2057】The Lost House
- POJ 2057 The Lost House 树形DP+贪心
- POJ 2057 The Lost House 经典树形DP+贪心
- 【树状数组】PKU-2057-The Lost House
- pku2057 The Lost House
- POJ 2057 The Lost House 树型dp+贪心思想在动态规划上的应用
- 【Poj 2507】The Lost House(树型dp)
- 【POJ2057】The Lost House【TreeDP】
- The Lost House--经典树形DP
- poj_2057 The Lost House(树形dp)
- 水仙花数
- Dlib人脸特征点检测(速度优化)
- MySQL数据库操作
- HDOJ 1234 开门人和关门人 (结构体排序)
- Begin 写博客啦,将自己每天的收获记录下来
- poj 2057 The Lost House
- Activity
- Semaphore实现信号灯
- Android Studio运行时finished with non-zero exit value 1 或:app:transformClassesWithDexForNduoDebug
- Linux内核的整体架构
- 线程池初学
- 素数求和
- 理清ThinkPHP的目录结构及访问规则,创建第一个控制器
- nyoj 2 括号配对 栈