原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ351.html

题目传送门 - UOJ351

题意

  有一个 n 个节点的树,每次涂黑一个叶子节点(度为 1 的节点),可以重复涂黑。

  问使得白色部分的直径发生变化的期望涂黑次数。

  $n\leq 5\times 10^5$

题解

  首先考虑什么情况下直径长度会发生改变。

  考虑找到直径的中点,可能在边上。

  对于这个直径相连的每一个子树,分别算出在这个子树中的距离直径中点距离为直径长度的一半的节点个数。

  于是我们就得到了一些集合。那么,直径长度将在 只剩下一个集合有白色节点 的时候发生改变。

  于是,很容易得出菊花图的做法:

$$ ans = \sum_{i=1}^{n-1} \frac {n-1} {n-i}$$

  但是不是菊花图的时候,仍然很棘手。

  设总叶子节点个数为 $l$ ,所有集合的元素个数总和为 $tot$ 。

官方题解的算法三应该比较好理解吧

算法三

我们考虑枚举哪个集合最终剩下了,设当前集合大小为 $k$ 。

设所有集合大小的和为 $n$ 。

我们枚举当其他所有集合都染黑时,当前集合还剩下 $i$ 个没有被染黑,那么这样的情况对答案的贡献为

$$\frac{\binom{k}{i}\times \binom{n-i-1}{n-k-1}\times \binom{n-k}{1}\times(k-i)!\times(n-k-1)!\times (i)!}{n!}\times (\sum_{j=i+1}^{n}\frac{m}{j})$$

线性预处理阶乘,阶乘逆元与逆元的前缀和就可以做到 $O(n)$ 的时间复杂度。

算法四比较神仙

算法四

from WuHongxun

http://wuhongxun.blog.uoj.ac/blog/3345

放链接(逃

  对于算法四,我再补充一句:

考虑任何一个方案里我们染黑到所有集合都变黑了为止,它对答案的贡献是多少呢?如果 x 是这个方案里最后被染黑的集合,那么是倒数第二个被染黑的时间,反之则是最后一个集合被染黑的时间。

  这里,如果 x 不是最后一个被染黑的集合,那么必然全部染黑了。

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int read(){
  4. int x=0;
  5. char ch=getchar();
  6. while (!isdigit(ch))
  7. ch=getchar();
  8. while (isdigit(ch))
  9. x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
  10. return x;
  11. }
  12. const int N=500005,mod=998244353;
  13. int n;
  14. vector <int> e[N],S;
  15. int in[N],depth[N],fa[N];
  16. int vis[N];
  17. void dfs(int x,int pre,int d){
  18. depth[x]=d,fa[x]=pre;
  19. for (auto y : e[x])
  20. if (y!=pre)
  21. dfs(y,x,d+1);
  22. }
  23. int LCA(int x,int y){
  24. if (depth[x]<depth[y])
  25. swap(x,y);
  26. while (depth[x]>depth[y])
  27. x=fa[x];
  28. while (x!=y)
  29. x=fa[x],y=fa[y];
  30. return x;
  31. }
  32. int FindFar(int x,int d){
  33. vis[x]=d;
  34. int res=x;
  35. for (auto y : e[x])
  36. if (!~vis[y]){
  37. int tmp=FindFar(y,d+1);
  38. if (vis[tmp]>vis[res])
  39. res=tmp;
  40. }
  41. return res;
  42. }
  43. void Get(int x,int d,int md){
  44. if (d==md)
  45. S.back()++;
  46. vis[x]=1;
  47. for (auto y : e[x])
  48. if (!vis[y])
  49. Get(y,d+1,md);
  50. }
  51. void Get_S(){
  52. memset(vis,-1,sizeof vis);
  53. int s=FindFar(1,0);
  54. memset(vis,-1,sizeof vis);
  55. int t=FindFar(s,0);
  56. if (depth[s]<depth[t])
  57. swap(s,t);
  58. int d=depth[s]+depth[t]-2*depth[LCA(s,t)];
  59. memset(vis,0,sizeof vis);
  60. if (d&1){
  61. for (int i=d/2;i--;)
  62. s=fa[s];
  63. t=fa[s];
  64. vis[s]=vis[t]=1;
  65. S.push_back(0),Get(s,0,d/2);
  66. S.push_back(0),Get(t,0,d/2);
  67. }
  68. else {
  69. for (int i=d/2;i--;)
  70. s=fa[s];
  71. vis[s]=1;
  72. for (auto y : e[s])
  73. S.push_back(0),Get(y,1,d/2);
  74. }
  75. }
  76. int Pow(int x,int y){
  77. int ans=1;
  78. for (;y;y>>=1,x=1LL*x*x%mod)
  79. if (y&1)
  80. ans=1LL*ans*x%mod;
  81. return ans;
  82. }
  83. int Fac[N],Inv[N],Iv[N],h[N];
  84. void Math_Prework(){
  85. for (int i=Fac[0]=1;i<=n;i++)
  86. Fac[i]=1LL*Fac[i-1]*i%mod;
  87. Inv[n]=Pow(Fac[n],mod-2);
  88. for (int i=n;i>=1;i--)
  89. Inv[i-1]=1LL*Inv[i]*i%mod;
  90. for (int i=1;i<=n;i++)
  91. Iv[i]=1LL*Inv[i]*Fac[i-1]%mod;
  92. for (int i=1;i<=n;i++)
  93. h[i]=(h[i-1]+Iv[i])%mod;
  94. }
  95. int C(int n,int m){
  96. if (m<0||m>n)
  97. return 0;
  98. return 1LL*Fac[n]*Inv[m]%mod*Inv[n-m]%mod;
  99. }
  100. int main(){
  101. n=read();
  102. for (int i=1;i<n;i++){
  103. int a=read(),b=read();
  104. e[a].push_back(b);
  105. e[b].push_back(a);
  106. in[a]++,in[b]++;
  107. }
  108. dfs(1,0,0);
  109. Get_S();
  110. int l=0;
  111. for (int i=1;i<=n;i++)
  112. if (in[i]==1)
  113. l++;
  114. Math_Prework();
  115. int tot=0;
  116. for (auto y : S)
  117. tot+=y;
  118. // 设当前剩余 k 个,总共有 l 个,选出 x 个数:
  119. // l/k + l/(k-1) + ... + l/(k-x+1)
  120. // l * (h[k]-h[k-x])
  121. int ans=0;
  122. for (auto y : S)
  123. ans=(1LL*h[tot-y]+ans)%mod;
  124. ans=(-1LL*((int)S.size()-1)*h[tot]%mod+ans+mod)%mod;
  125. ans=1LL*ans*l%mod;
  126. printf("%d",ans);
  127. return 0;
  128. }

UOJ#351. 新年的叶子 概率期望的更多相关文章

  1. [UOJ#351]新年的叶子

    [UOJ#351]新年的叶子 试题描述 躲过了AlphaGo 之后,你躲在 SingleDog 的长毛里,和它们一起来到了AlphaGo 的家.此时你们才突然发现,AlphaGo 的家居然是一个隐藏在 ...

  2. uoj#351. 新年的叶子(概率期望)

    传送门 数学还是太差了,想了半天都没想出来 首先有一个定理,如果直径(这里考虑经过的点数)为奇数,所有直径有同一个中点,如果直径为偶数,所有直径有同一条最中间的边.这个可以用反证法,假设不成立的话直径 ...

  3. UOJ#299. 【CTSC2017】游戏 线段树 概率期望 矩阵

    原文链接www.cnblogs.com/zhouzhendong/p/UOJ299.html 前言 不会概率题的菜鸡博主做了一道概率题. 写完发现运行效率榜上的人都没有用心卡常数——矩阵怎么可以用数组 ...

  4. 「UOJ351」新年的叶子

    「UOJ351」新年的叶子 题目描述 有一棵大小为 \(n\) 的树,每次随机将一个叶子染黑,可以重复染,问期望染多少次后树的直径会缩小. \(1 \leq n \leq 5 \times 10^5\ ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 信息摘要算法之四:SHA512算法分析与实现

    前面一篇中我们分析了SHA256的原理,并且实现了该算法,在这一篇中我们将进一步分析SHA512并实现之. 1.SHA简述 尽管在前面的篇章中我们介绍过SHA算法,但出于阐述的完整性我依然要简单的说明 ...

  2. jquery easyui datagrid 加每页合计和总合计

    jquery easyui datagrid 加每页合计和总合计 一:效果图 二:代码实现 这个只有从后台来处理 后台根据rows 和page两个参数返回的datatable 命名为dt 然后根据dt ...

  3. Confluence 6 workbox 包含从 Jira 来的通知

    如果你的 Confluence 站点链接了一个 Jira 应用,你可以包含从 Jira 应用来的通知,例如 Jira 软化或 Jira 服务器桌面. 希望包含有从 Jira 应用来的通知: 你的 Ji ...

  4. linux和windows下,C/C++开发的延时函数,sleep函数

    简介: 函数名: sleep   功 能: 执行挂起一段时间   用 法: unsigned sleep(unsigned seconds);   在VC中使用带上头文件   #include < ...

  5. poj3254 炮兵阵地弱化版,记数类dp

    /* dp[i][j]表示到第i行的状态j有多少放置方式 */ #include<iostream> #include<cstring> #include<cstdio& ...

  6. CentOS下将Python的版本升级为3.x

    本文主要介绍在Linux(CentOS)下将Python的版本升级为3.x的方法 众所周知,在2020年python官方将不再支持2.7版本的python,所以使用3.x版本的python是必要的,但 ...

  7. iOS 测试之非代码获取 iPhone 型号及其他信息

    首先 安装libimobiledevice和ideviceinstaller $ brew uninstall ideviceinstaller $ brew uninstall libimobile ...

  8. spring cloud Hystrix监控面板Hystrix Dashboard和Turbine

    我们提到断路器是根据一段时间窗内的请求情况来判断并操作断路器的打开和关闭状态的.而这些请求情况的指标信息都是HystrixCommand和HystrixObservableCommand实例在执行过程 ...

  9. Command 'ifconfig' not found, but can be installed with: sudo apt install net-tools

    然后按照错误信息安安装网络工具: sudo apt install net-tools shl@shl-tx:~$ sudo apt install net-tools正在读取软件包列表... 完成正 ...

  10. 该问题是需要导包!!!需要pom中添加依赖The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application

    <!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl --><depend ...