TCO14 1B L3: EagleInZoo, dp,tree
题目: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的更多相关文章
- hdu Anniversary party 树形DP,点带有值。求MAX
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- DP Intro - Tree DP Examples
因为上次比赛sb地把一道树形dp当费用流做了,受了点刺激,用一天时间稍微搞一下树形DP,今后再好好搞一下) 基于背包原理的树形DP poj 1947 Rebuilding Roads 题意:给你一棵树 ...
- DP Intro - Tree POJ2342 Anniversary party
POJ 2342 Anniversary party (树形dp 入门题) Anniversary party Time Limit: 1000MS Memory Limit: 65536K To ...
- 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 ...
- LuoguP4719 【模板】动态 DP(动态DP,LCT)
\(n \times m\)的算法谁都会吧,注意到每次修改影响的仅是一部分的信息,因此可思考优化. 将每个节点对应一个矩阵\(\begin{bmatrix} g[v][0] & g[v][0] ...
- 【NOI P模拟赛】最短路(树形DP,树的直径)
题面 给定一棵 n n n 个结点的无根树,每条边的边权均为 1 1 1 . 树上标记有 m m m 个互不相同的关键点,小 A \tt A A 会在这 m m m 个点中等概率随机地选择 k k k ...
- HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)
题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...
- HDU 2577 How to Type (DP,经典)
题意: 打字游戏,求所按的最少次数.给出一个串,其中有大小写,大写需要按下cap键切换到大写,或者在小写状态下按shift+键,这样算两次,打小写时则相反.注意:在打完所有字后,如果cap键是开着的, ...
- Android中如何将dp,dip,sp与px相互转化
Android中有很多度量单位:比如常用的dp,dip,sp,px等,有时候需要将他们相互转换,有下面非常方便的方法: 比如sp转换成px: TypedValue.applyDimension(Typ ...
随机推荐
- [CSS3] The different of Background-size between 'cover' and 'contain'
'cover': The smaller axies of image (x axies) should match smaller axies (x axies) of container. So ...
- [Angular] Read Custom HTTP Headers Sent by the Server in Angular
By default the response body doesn’t contain all the data that might be needed in your app. Your ser ...
- log4j.xml打印日志信息(2)
log4j.xml文件 <? xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:con ...
- 关于Android的.so文件所须要知道的
早期的Android系统差点儿仅仅支持ARMv5的CPU架构,你知道如今它支持多少种吗?7种. Android系统眼下支持以下七种不同的CPU架构:ARMv5.ARMv7 (从2010年起),x86 ...
- 20.boost dijkstra最短路径算法
到某个点的最短距离 到终点的最短路径 完整代码 #include <iostream> #include <string> #include &l ...
- MetaSploit攻击实例讲解------社会工程学set攻击(kali linux 2016.2(rolling))(详细)
不多说,直接上干货! 首先,如果你是用的BT5,则set的配置文件是在 /pentest/exploits/set/set_config下. APACHE_SERVER=ONSELF_SIGNED_A ...
- CentOS_mysql8.0_错误
#参考资料 CSND:https://blog.csdn.net/y_server/article/details/78781177 博客园:http://www.cnblogs.com/testwa ...
- pthread_cleanup_push vs Autorelease VS 异常处理
黑幕背后的Autorelease http://www.cnblogs.com/feng9exe/p/7239552.html objc_autoreleasePoolPush的返回值正是这个哨兵对象 ...
- Hadoop2.x 关于日志文件位置
查看日志是发现Hadoop问题和解决Hadoop问题的第一步. 开始我不知道该去哪找日志,后来我发现在我启动节点的时候,有打印信息以及明确告诉了日志写在哪. [root@master hadoop]# ...
- luogu P3396 哈希冲突(分块?)
我们可以维护一个\(f[i][j]\)代表%\(i\)意义下得\(j\)的答案.然后维护就炸了. 先设\(x=\sqrt{n}\)然后我们发现,当\(i>x\)时我们直接暴力复杂度为\(O(x) ...