题目:http://community.topcoder.com/stat?c=problem_statement&pm=13117&rd=15950

看到树,又是与节点有关,通常是dp,dp[v][t] 表示在以v为root的subtree上有t个eagle时满足条件的概率。一个注意点是求二项系数数组C[]时,由于值太大。要用double,不能用long long, long long 会溢出。

#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iostream>
#include <sstream>
#include <iomanip> #include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map> #include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std; #define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)
typedef pair<int, int> pii;
typedef long long llong;
typedef pair<llong, llong> pll;
#define mkp make_pair /*************** Program Begin **********************/
const int MAX_N = 51, MAX_K = 101;
double dp[MAX_N][MAX_K];
double C[MAX_K][MAX_K]; // 二项系数,注意:用 long long 会溢出
class EagleInZoo {
public:
vector < vector<int> > childs;
double rec(int v, int t)
{
if (1 == t) {
return 1.0;
}
double & res = dp[v][t];
if (res > -0.5) {
return res;
}
if (childs[v].size() == 0 && t > 1) { // 该节点无 child 且t > 1。最后一仅仅一定无法停留
res = 0;
return res;
}
res = 0; for (int i = 0; i < childs[v].size(); i++) {
int x = childs[v][i];
int c = childs[v].size();
for (int j = 0; j <= t - 2; j++) {
res += (1.0 / c) * C[t-2][j] * pow(1.0 / c, j) * pow( 1.0 * (c-1) / c, t-2-j) * rec(x, j + 1);
}
} return res;
}
double calc(vector <int> parent, int K) {
for (int i = 0; i < MAX_N; i++) {
for (int j = 0; j < MAX_K; j++) {
dp[i][j] = -1.0;
}
}
childs.resize(parent.size() + 1);
for (int i = 0; i < parent.size(); i++) {
childs[ parent[i] ].push_back(i + 1);
}
// calculate C[]
C[0][0] = 1;
for (int i = 1; i <= K - 1; i++) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; j++) {
C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
}
}
return rec(0, K);
} }; /************** Program End ************************/

TCO14 1B L3: EagleInZoo, dp,tree的更多相关文章

  1. hdu Anniversary party 树形DP,点带有值。求MAX

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  2. DP Intro - Tree DP Examples

    因为上次比赛sb地把一道树形dp当费用流做了,受了点刺激,用一天时间稍微搞一下树形DP,今后再好好搞一下) 基于背包原理的树形DP poj 1947 Rebuilding Roads 题意:给你一棵树 ...

  3. DP Intro - Tree POJ2342 Anniversary party

    POJ 2342 Anniversary party (树形dp 入门题) Anniversary party Time Limit: 1000MS   Memory Limit: 65536K To ...

  4. HDU 1693 Eat the Trees(插头DP,入门题)

    Problem Description Most of us know that in the game called DotA(Defense of the Ancient), Pudge is a ...

  5. LuoguP4719 【模板】动态 DP(动态DP,LCT)

    \(n \times m\)的算法谁都会吧,注意到每次修改影响的仅是一部分的信息,因此可思考优化. 将每个节点对应一个矩阵\(\begin{bmatrix} g[v][0] & g[v][0] ...

  6. 【NOI P模拟赛】最短路(树形DP,树的直径)

    题面 给定一棵 n n n 个结点的无根树,每条边的边权均为 1 1 1 . 树上标记有 m m m 个互不相同的关键点,小 A \tt A A 会在这 m m m 个点中等概率随机地选择 k k k ...

  7. HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)

    题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...

  8. HDU 2577 How to Type (DP,经典)

    题意: 打字游戏,求所按的最少次数.给出一个串,其中有大小写,大写需要按下cap键切换到大写,或者在小写状态下按shift+键,这样算两次,打小写时则相反.注意:在打完所有字后,如果cap键是开着的, ...

  9. Android中如何将dp,dip,sp与px相互转化

    Android中有很多度量单位:比如常用的dp,dip,sp,px等,有时候需要将他们相互转换,有下面非常方便的方法: 比如sp转换成px: TypedValue.applyDimension(Typ ...

随机推荐

  1. H5知识点

    一.总体变化 1.H5文档结构 <!DOCTYPE html> <html> <head> <title>  这是标题  </title> ...

  2. 聚类算法学习-kmeans,kmedoids,GMM

    GMM参考这篇文章:Link 简单地说,k-means 的结果是每个数据点被 assign 到其中某一个 cluster 了,而 GMM 则给出这些数据点被 assign 到每个 cluster 的概 ...

  3. FZU_Problem 2168 防守阵地 I

    Problem 2168 防守阵地 I Accept: 128 Submit: 392 Time Limit: 3000 mSec Memory Limit : 32768 KB Problem De ...

  4. 【MVC架构】——怎样利用Json在View和Controller之间传递数据

    在MVC架构中,尽管非常多东西和三层非常相似,可是也有非常大的差别.就比方传递数据.在三层架构中,传递数据就仅仅要一层返回,另外一层用同样类型的变量来接收即可了.在MVC中,事实上原理是一样的,Con ...

  5. [HEOI2016/TJOI2016] 排序 解题报告(二分答案/线段树分裂合并+set)

    题目链接: https://www.luogu.org/problemnew/show/P2824 题目描述: 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在 ...

  6. 133.throw机制 抛出类类型

    #include <iostream> using namespace std; //try尝试执行,抛出throw,throw之后语句不再执行 //catch处理throw的异常 voi ...

  7. Ubuntu下gcc安装

    在Ubuntu下安装GCC和其他一些Linux系统有点不一样. 方法一: 该方法超简单:sudo apt-get  build-depgcc 就上面这条命令就可以搞定 方法二:sudo apt-get ...

  8. solarwinds之网络发现

    1.  首先需要添加网络发现   2.  使用public   3.  添加主机   4.  网络地址选择   5.  默认下一步   6.  运行发现   7.  扫描结构如下   8.  下一步 ...

  9. SpringCloud学习笔记(15)----Spring Cloud Netflix之Hystrix Dashboard的使用

    1. 引入依赖 在前面几节中的消费者中添加pom依赖. <dependency> <groupId>org.springframework.cloud</groupId& ...

  10. 3ds Max制作妄想中的外星人形象

    来源:CG游 作者:FedericoScarbini 使用软件:3ds Max, Photoshop, ZBrush 简介 我认为每一个人都曾经在他的人生中的某些时刻妄想着关于外星人的事情;我猜这是很 ...