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. Linux下Redis C++操作的封装

    安装和启动Redis服务...略!很粗糙的版本,待改进... Redis Client C++示例代码...略! /** * Time: 14-3-10 * File: RedisCache.h * ...

  2. break和continue 都是指的最接近的内层循环

    break和continue 都是指的最接近的内层循环

  3. void类型和void *指针类型(网上摘抄总结)【转】

    http://www.blogjava.net/fhtdy2004/archive/2009/07/09/286004.html 现在在学linux编程过程中遇到很多void *指针类型,由于c很早学 ...

  4. Css的使用细谈

    Css的使用细谈 Css可以通过简单的更改CSS文件,改变网页的整体表现形式,可以减少我们的工作量,所以她是每一个网页设计人员的必修课. Css简介              (1) CSS是用于布局 ...

  5. ubuntu166.04之Caffe安装

    写在前面:之前一直在搞keras,最近由于某些需求,需要学习caffe,在此记录caffe的安装记录.默认已经安装了cuda 如果是从其他的深度学习平台迁移到Caffe,那么按照这个教程来就可以了. ...

  6. 51nod最长递增路径:(还不错的图)

    一个无向图,可能有自环,有重边,每条边有一个边权.你可以从任何点出发,任何点结束,可以经过同一个点任意次.但是不能经过同一条边2次,并且你走过的路必须满足所有边的权值严格单调递增,求最长能经过多少条边 ...

  7. mysql函数之八:mysql函数大全

    对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的最左面字符的ASCII代码值.如果str是空字符串,返回0.如果str是NULL,返回NULL. mysql& ...

  8. libvirt kvm云主机监控

    libvirt

  9. Asset Catalog Help (十)---Specifying a Resizable Area of an Image

    Specifying a Resizable Area of an Image Use the Xcode Slicing feature to specify the dimensions of a ...

  10. error:: undefined reference to symbol '__glewBufferSubData' 未定义的引用 以及 error: main.o: undefined reference to symbol 'glTexImage2D'

    在把DSO移植到QT工程中,出现了 /usr/bin/ld: KeyFrameDisplay.o: undefined reference to symbol '__glewBufferSubData ...