ZOJ3329有些像,都是用期望列出来式子以后,为了解式子,设A[i],B[i],此题又多了C[i],然后用递推(此题是树形dp)去求得ABC,最后结果只跟ABC有关,跟列写的期望数组根本无关。

虽然式子很长很冗,但平心而论思维上并不难理解,关键是自信和耐心去带入。ABC的递推式出来了以后,代码就不难了。

据说eps有坑?

邝斌巨巨的:

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <fstream>
#include <bitset>
#define init(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define irep(i, a, b) for (int i = a; i >= b; i--)
using namespace std; typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int inf = 0x3f3f3f3f;
const ll INF = 1e18; template <typename T> void read(T &x) {
x = ;
int s = , c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') s = -;
for (; isdigit(c); c = getchar())
x = x * + c - ;
x *= s;
} template <typename T> void write(T x) {
if (x < ) x = -x, putchar('-');
if (x > ) write(x / );
putchar(x % + '');
} template <typename T> void writeln(T x) {
write(x);
puts("");
} const int maxn = 1e4 + ;
const db eps = 1e-;
int T, n, kase;
db A[maxn], B[maxn], C[maxn], K[maxn], E[maxn];
vector<int> v[maxn]; bool dfs(int cur, int fa) {
int m = v[cur].size();
if (m == && fa) {
A[cur] = K[cur];
B[cur] = C[cur] = 1.0 - K[cur] - E[cur];
} else {
db tmp = ( - K[cur] - E[cur]) / m;
db Atmp = , Btmp = , Ctmp = ;
for (int child : v[cur]) {
if (child != fa) {
if (!dfs(child, cur)) return false;
Atmp += A[child];
Btmp += B[child];
Ctmp += C[child];
}
}
if (fabs( - tmp * Btmp) < eps) return false;
A[cur] = (K[cur] + tmp * Atmp) / ( - tmp * Btmp);
B[cur] = tmp / ( - tmp * Btmp);
C[cur] = (tmp * Ctmp + - K[cur] - E[cur]) / ( - tmp * Btmp);
}
return true;
} int main() {
for (read(T); T; T--) {
read(n);
rep(i, , n) v[i].clear();
rep(i, , n - ) {
int x, y;
read(x), read(y);
v[x].push_back(y);
v[y].push_back(x);
}
rep(i, , n) {
read(K[i]), read(E[i]);
K[i] /= , E[i] /= ;
} printf("Case %d: ", ++kase);
if (dfs(, ) && fabs( - A[]) > eps) {
printf("%.6lf\n", C[] / ( - A[]));
} else {
puts("impossible");
}
}
return ;
}

HDU4035(概率期望、树形、数学)的更多相关文章

  1. luoguP4284 [SHOI2014]概率充电器 概率期望树形DP

    这是一道告诉我概率没有想象中那么难的题..... 首先,用期望的线性性质,那么答案为所有点有电的概率和 发现一个点的有电的概率来源形成了一个"或"关系,在概率中,这并不好计算... ...

  2. 牛客网多校赛第9场 E-Music Game【概率期望】【逆元】

    链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...

  3. 概率与期望详解!一次精通oi中的概率期望

    目录 基础概念 最大值不超过Y的期望 概率为P时期望成功次数 基础问题 拿球 随机游走 经典问题 期望线性性练习题 例题选讲 noip2016换教室 区间交 0-1边树求直径期望 球染色 区间翻转 二 ...

  4. 【BZOJ-1419】Red is good 概率期望DP

    1419: Red is good Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 660  Solved: 257[Submit][Status][Di ...

  5. uvalive 7331 Hovering Hornet 半平面交+概率期望

    题意:一个骰子在一个人正方形内,蜜蜂在任意一个位置可以出现,问看到点数的期望. 思路:半平面交+概率期望 #include<cstdio> #include<cstring> ...

  6. OI队内测试一【数论概率期望】

    版权声明:未经本人允许,擅自转载,一旦发现将严肃处理,情节严重者,将追究法律责任! 序:代码部分待更[因为在家写博客,代码保存在机房] 测试分数:110 本应分数:160 改完分数:200 T1: 题 ...

  7. 2016 多校联赛7 Balls and Boxes(概率期望)

    Mr. Chopsticks is interested in random phenomena, and he conducts an experiment to study randomness. ...

  8. 【bzoj4832】[Lydsy2017年4月月赛]抵制克苏恩 概率期望dp

    题目描述 你分别有a.b.c个血量为1.2.3的奴隶主,假设英雄血量无限,问:如果对面下出一个K点攻击力的克苏恩,你的英雄期望会受到到多少伤害. 输入 输入包含多局游戏. 第一行包含一个整数 T (T ...

  9. [LnOI2019]加特林轮盘赌(DP,概率期望)

    [LnOI2019]加特林轮盘赌(DP,概率期望) 题目链接 题解: 首先特判掉\(p=0/1\)的情况... 先考虑如果\(k=1\)怎么做到\(n^2\)的时间复杂度 设\(f[i]\)表示有\( ...

  10. 【loj6191】「美团 CodeM 复赛」配对游戏 概率期望dp

    题目描述 n次向一个栈中加入0或1中随机1个,如果一次加入0时栈顶元素为1,则将这两个元素弹栈.问最终栈中元素个数的期望是多少. 输入 一行一个正整数 n . 输出 一行一个实数,表示期望剩下的人数, ...

随机推荐

  1. 关于JavaScript的事件触发

    突然知道JavaScript底层是怎么实现事件触发的,找到一个博客,功力不够,看的很迷糊,记载这里吧,后面再研究. [探讨]javascript事件机制底层实现原理

  2. zkui部署

    1.拉取代码 #git clone https://github.com/DeemOpen/zkui.git 2.构建并安装程序 #cd zkui/ #yum install -y maven #mv ...

  3. MYSQL进阶学习笔记十四:MySQL 应用程序优化!(视频序号:进阶_32)

    知识点十五:MySQL 的应用程序优化(32) 一.访问数据库采用连接池 把连接当做对象或设备,统一放在‘连接池’里.凡是需要访问数据库的地方都从连接池里取连接 二.采用缓存减少对于MySQL的访问: ...

  4. hdu 1711 Number Sequence(kmp找子串第一次出现的位置)

    题意:裸kmp 思路:kmp模板 #include<iostream> #include<stdio.h> #include<string.h> using nam ...

  5. vertical-align 的理解

    1.vertical-align 属性和值列表

  6. 最基本PSO算法的C++实现

    按照James Kennedy & Russell Eberhart (1995)的版本,算法过程如下: [x*] = PSO() P = Particle_Initialization(); ...

  7. CNN中下一层Feature map大小计算

    符号表示: $W$:表示当前层Feature map的大小. $K$:表示kernel的大小. $S$:表示Stride的大小. 具体来讲: 整体说来,和下一层Feature map大小最为密切的就是 ...

  8. [WC 2006] 水管局长

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2594 [算法] 首先离线 , 将删边操作转化为倒序加边 假设我们已经维护出了一棵最小 ...

  9. node之get与post

    Get获取内容: var http=require('http'), util=require('util'),//util 提供常用函数集合 url=require('url'); http.cre ...

  10. java面试编程题

      [程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?   //这是一个菲波拉契数列问 ...