题目描述

给定一棵\(n\)个节点的树,每条边的权值为\([0,L]\)之间的随机整数,求这棵树两点之间最长距离不超过\(S\)的概率。

Input

第一行三个整数\(n,L,S\)

接下来n-1行,每行两个整数,表示树上的一条边。

  1. \(1 <= T <= 512\)
  2. \(1 <= N <= 64\)
  3. \(1 <= L <= 8, 1 <= S <= 512\)

加强版:

  1. \(T=1\)
  2. \(1 <= N <= 100\)
  3. \(1 <= L <= 10, 1 <= S <= 2000\)

Output

输出树上任意两点距离都不超过\(S\)的概率,误差在\(10^{-6}\)以内都在算正确

Sample Input

3
2 3 2
1 2
4 3 4
1 2
2 3
3 4
7 4 10
1 2
2 3
4 5
2 6
4 7
4 6

Sample Output

Case 1: 0.750000
Case 2: 0.500000
Case 3: 0.624832

让我们先来考虑一下未加强的数据该怎么去写?

题目中树上两点最大距离就是树的直径。

因此,对于一个节点,我们需要维护信息是该节点到其子树的最大值的距离的概率。

同时,我们还要把儿子向父亲转移,即维护儿子到另一个儿子的距离的概率。

那么,我们就可以直接树形\(DP\)了。

\(dp[i][j]\)表示节点\(i\)到其子树的最大值为\(j\)的概率。

\(Dis[i]\)表示当前节点的子树到父亲的距离为\(i\)的概率。

\(temp[i]\)进行储存\(dp\)值,因为一个\(dp\)值会被多次利用。

总时间复杂度:\(O(n*S*(S+L))\)。

对于这个算法是无法过加强版的。

我们可以发现,这个\(dp\)的时间复杂度主要累积到\(temp\)值的累积上。

并且\(temp\)值的累积一定是连续的一段区间。

于是我们就可以利用前缀和优化,最后再把重复的给减掉。

时间复杂度:\(O(n*S*L)\)。

代码如下

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define u64 unsigned long long
#define reg register
#define Raed Read
#define debug(x) cerr<<#x<<" = "<<x<<endl;
#define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a)
#define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a)
#define drep(a,b,c) for(reg int a=(b),a##_end_=(c); a>=a##_end_; --a)
#define erep(i,G,x) for(reg int i=(G).Head[x]; i; i=(G).Nxt[i]) inline int Read() {
int res = 0, f = 1;
char c;
while (c = getchar(), c < 48 || c > 57)if (c == '-')f = 0;
do res = (res << 3) + (res << 1) + (c ^ 48);
while (c = getchar(), c >= 48 && c <= 57);
return f ? res : -res;
} template<class T>inline bool Min(T &a, T const&b) {
return a > b ? a = b, 1 : 0;
}
template<class T>inline bool Max(T &a, T const&b) {
return a < b ? a = b, 1 : 0;
} const int N = 2e3+5, M = 1005, mod = 1e9 + 7; bool MOP1; int n,l,s,Case; struct Link_list {
int Tot,Head[N],Nxt[M<<1],to[M<<1],cost[M<<1];
inline void Init(void) {
Tot=0;
memset(Head,0,sizeof Head);
}
inline void AddEdgepair(int a,int b) {
to[++Tot]=b,Nxt[Tot]=Head[a],Head[a]=Tot;
to[++Tot]=a,Nxt[Tot]=Head[b],Head[b]=Tot;
}
} G; struct T290 {
double Dis[N],dp[N][N],temp[N];
void dfs(int x,int pre) {
rep(i,1,s)dp[x][i]=0;
dp[x][0]=1;
rep(j,0,s)Dis[j]=temp[j]=0;
erep(i,G,x) {
int y=G.to[i];
if(y==pre)continue;
dfs(y,x);
rep(j,0,s)Dis[j]=temp[j]=0;
rep(j,0,l)rep(k,0,s)if(j+k<=s)Dis[j+k]+=dp[y][k]*1.0/(l+1);
else break;
rep(j,0,s)rep(k,0,s-j)temp[max((int)k,(int)j)]+=dp[x][k]*Dis[j];
rep(j,0,s)dp[x][j]=temp[j];
}
}
inline void solve(void) {
ret(i,1,n) {
int a=Raed(),b=Read();
G.AddEdgepair(a,b);
}
dfs(1,0);
double Ans=0;
rep(i,0,s)Ans+=dp[1][i];
printf("Case %lld: %.6lf\n",++Case,Ans);
}
} Pbl; struct T2100 {
double Dis[N],dp[N][N],temp[N],Sum1[N],Sum2[N];
void dfs(int x,int pre) {
rep(i,1,s)dp[x][i]=0;
dp[x][0]=1;
erep(i,G,x) {
int y=G.to[i];
if(y==pre)continue;
dfs(y,x);
rep(j,0,s)Dis[j]=temp[j]=0;
rep(j,0,l)rep(k,0,s)if(j+k<=s)Dis[j+k]+=dp[y][k]*1.0/(l+1);
else break;
Sum1[0]=dp[x][0];
Sum2[0]=Dis[0];
rep(i,1,s) {
Sum1[i]=Sum1[i-1]+dp[x][i];
Sum2[i]=Sum2[i-1]+Dis[i];
}
rep(j,0,s) {
int k=min(j,s-j);
temp[j]+=Sum1[k]*Dis[j];
}
rep(j,0,s) {
int k=min(j,s-j);
temp[j]+=Sum2[k]*dp[x][j];
}
rep(j,0,s)if(j*2<=s)temp[j]-=dp[x][j]*Dis[j];
rep(j,0,s)dp[x][j]=temp[j];
}
}
inline void solve(void) {
ret(i,1,n) {
int a=Raed(),b=Read();
G.AddEdgepair(a,b);
}
dfs(1,0);
double Ans=0;
rep(i,0,s)Ans+=dp[1][i];
printf("Case %lld: %.6lf\n",++Case,Ans);
}
} P100; bool MOP2; inline void _main(void) {
int T=Read();
while(T--) {
G.Init();
n=Raed(),l=Read(),s=Read();
P100.solve();
}
} signed main() {
#define offline1
#ifdef offline
freopen("random.in", "r", stdin);
freopen("random.out", "w", stdout);
_main();
fclose(stdin);
fclose(stdout);
#else
_main();
#endif
return 0;
}

HDU-4219-Randomization?的更多相关文章

  1. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  3. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  4. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  5. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  6. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  7. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  8. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  9. hdu 4329

    problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟  a.     p(r)=   R'/i   rel(r)=(1||0)  R ...

  10. HDU 2586

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:求最近祖先节点的权值和 思路:LCA Tarjan算法 #include <stdio.h&g ...

随机推荐

  1. Eclipse中导入两个相同的工程

    问题描述:同时在Eclipse中导入两个相同的工程,会有 Some projects cannot be imported because they already exist in the work ...

  2. TTTTTTTTTTTTTT CF 645D 点的优先级

    题意:给你n个节点,m对优先级关系,a[i] b[i]代表a[i]的优先级比b[i]高,现在问你至少需要前多少对关系就能确定所有节点的优先级: #include <iostream> #i ...

  3. 3.JSP

        JSP(Java Server Pages)页面是指扩展名为.jsp的文件,在一个JSP中可以包含指令标识,HTML代码, JavaScript代码,嵌入的Java代码,注释和JSP动作标识等 ...

  4. 小程序开发获取token值

    // 登录 wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId // console.lo ...

  5. 图解数据库中的join操作

    1.所有的join都从cross join衍生而来 2.所有join图示 转自Say NO to Venn Diagrams When Explaining JOINs

  6. T84341 Jelly的难题1

    T84341 Jelly的难题1 题解 当窝发现窝的锅在读入这个矩阵的时候,窝..窝..窝.. 果然,一遇到和字符串有关的题就开始吹空调 好啦我们说说思路吧 BFS队列实现 拿出一个没有走过的点,扩展 ...

  7. C++模板函数实践1

    实践如下: #include <iostream> #include <typeinfo> using namespace std; class Bean{ private: ...

  8. N2RR

    一.背景 氨(NH 3)是一种新兴的能量载体,在液氨中含有17.6%(重量)的氢,而在甲醇中的含量为12.5%(重量),很可能成为未来氢能经济的有希望的候选者.然而,如果基于NH 3的肥料不足以养活世 ...

  9. flutter stack嵌套,appbar透明,Container设置背景图片并且图片在appbar之下

    stack嵌套 一般情况下 stack是无法嵌套,出现stack嵌套,布局就会出乱 解决方式:就是第二个stack需要确定宽高 appbar透明 AppBar( backgroundColor: Co ...

  10. [Nova ERROR] InternalError: Nova requires QEMU version 2.5.0 or greater.

    目录 文章目录 目录 问题 调查 解决 问题 nova-compute service 启动失败 InternalError: Nova requires QEMU version 2.5.0 or ...