UOJ#351. 新年的叶子 概率期望
原文链接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 不是最后一个被染黑的集合,那么必然全部染黑了。
代码
- #include <bits/stdc++.h>
- using namespace std;
- int read(){
- int x=0;
- char ch=getchar();
- while (!isdigit(ch))
- ch=getchar();
- while (isdigit(ch))
- x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
- return x;
- }
- const int N=500005,mod=998244353;
- int n;
- vector <int> e[N],S;
- int in[N],depth[N],fa[N];
- int vis[N];
- void dfs(int x,int pre,int d){
- depth[x]=d,fa[x]=pre;
- for (auto y : e[x])
- if (y!=pre)
- dfs(y,x,d+1);
- }
- int LCA(int x,int y){
- if (depth[x]<depth[y])
- swap(x,y);
- while (depth[x]>depth[y])
- x=fa[x];
- while (x!=y)
- x=fa[x],y=fa[y];
- return x;
- }
- int FindFar(int x,int d){
- vis[x]=d;
- int res=x;
- for (auto y : e[x])
- if (!~vis[y]){
- int tmp=FindFar(y,d+1);
- if (vis[tmp]>vis[res])
- res=tmp;
- }
- return res;
- }
- void Get(int x,int d,int md){
- if (d==md)
- S.back()++;
- vis[x]=1;
- for (auto y : e[x])
- if (!vis[y])
- Get(y,d+1,md);
- }
- void Get_S(){
- memset(vis,-1,sizeof vis);
- int s=FindFar(1,0);
- memset(vis,-1,sizeof vis);
- int t=FindFar(s,0);
- if (depth[s]<depth[t])
- swap(s,t);
- int d=depth[s]+depth[t]-2*depth[LCA(s,t)];
- memset(vis,0,sizeof vis);
- if (d&1){
- for (int i=d/2;i--;)
- s=fa[s];
- t=fa[s];
- vis[s]=vis[t]=1;
- S.push_back(0),Get(s,0,d/2);
- S.push_back(0),Get(t,0,d/2);
- }
- else {
- for (int i=d/2;i--;)
- s=fa[s];
- vis[s]=1;
- for (auto y : e[s])
- S.push_back(0),Get(y,1,d/2);
- }
- }
- int Pow(int x,int y){
- int ans=1;
- for (;y;y>>=1,x=1LL*x*x%mod)
- if (y&1)
- ans=1LL*ans*x%mod;
- return ans;
- }
- int Fac[N],Inv[N],Iv[N],h[N];
- void Math_Prework(){
- for (int i=Fac[0]=1;i<=n;i++)
- Fac[i]=1LL*Fac[i-1]*i%mod;
- Inv[n]=Pow(Fac[n],mod-2);
- for (int i=n;i>=1;i--)
- Inv[i-1]=1LL*Inv[i]*i%mod;
- for (int i=1;i<=n;i++)
- Iv[i]=1LL*Inv[i]*Fac[i-1]%mod;
- for (int i=1;i<=n;i++)
- h[i]=(h[i-1]+Iv[i])%mod;
- }
- int C(int n,int m){
- if (m<0||m>n)
- return 0;
- return 1LL*Fac[n]*Inv[m]%mod*Inv[n-m]%mod;
- }
- int main(){
- n=read();
- for (int i=1;i<n;i++){
- int a=read(),b=read();
- e[a].push_back(b);
- e[b].push_back(a);
- in[a]++,in[b]++;
- }
- dfs(1,0,0);
- Get_S();
- int l=0;
- for (int i=1;i<=n;i++)
- if (in[i]==1)
- l++;
- Math_Prework();
- int tot=0;
- for (auto y : S)
- tot+=y;
- // 设当前剩余 k 个,总共有 l 个,选出 x 个数:
- // l/k + l/(k-1) + ... + l/(k-x+1)
- // l * (h[k]-h[k-x])
- int ans=0;
- for (auto y : S)
- ans=(1LL*h[tot-y]+ans)%mod;
- ans=(-1LL*((int)S.size()-1)*h[tot]%mod+ans+mod)%mod;
- ans=1LL*ans*l%mod;
- printf("%d",ans);
- return 0;
- }
UOJ#351. 新年的叶子 概率期望的更多相关文章
- [UOJ#351]新年的叶子
[UOJ#351]新年的叶子 试题描述 躲过了AlphaGo 之后,你躲在 SingleDog 的长毛里,和它们一起来到了AlphaGo 的家.此时你们才突然发现,AlphaGo 的家居然是一个隐藏在 ...
- uoj#351. 新年的叶子(概率期望)
传送门 数学还是太差了,想了半天都没想出来 首先有一个定理,如果直径(这里考虑经过的点数)为奇数,所有直径有同一个中点,如果直径为偶数,所有直径有同一条最中间的边.这个可以用反证法,假设不成立的话直径 ...
- UOJ#299. 【CTSC2017】游戏 线段树 概率期望 矩阵
原文链接www.cnblogs.com/zhouzhendong/p/UOJ299.html 前言 不会概率题的菜鸡博主做了一道概率题. 写完发现运行效率榜上的人都没有用心卡常数——矩阵怎么可以用数组 ...
- 「UOJ351」新年的叶子
「UOJ351」新年的叶子 题目描述 有一棵大小为 \(n\) 的树,每次随机将一个叶子染黑,可以重复染,问期望染多少次后树的直径会缩小. \(1 \leq n \leq 5 \times 10^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 ...
- uvalive 7331 Hovering Hornet 半平面交+概率期望
题意:一个骰子在一个人正方形内,蜜蜂在任意一个位置可以出现,问看到点数的期望. 思路:半平面交+概率期望 #include<cstdio> #include<cstring> ...
- OI队内测试一【数论概率期望】
版权声明:未经本人允许,擅自转载,一旦发现将严肃处理,情节严重者,将追究法律责任! 序:代码部分待更[因为在家写博客,代码保存在机房] 测试分数:110 本应分数:160 改完分数:200 T1: 题 ...
- 2016 多校联赛7 Balls and Boxes(概率期望)
Mr. Chopsticks is interested in random phenomena, and he conducts an experiment to study randomness. ...
- 牛客网多校赛第9场 E-Music Game【概率期望】【逆元】
链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...
随机推荐
- 信息摘要算法之四:SHA512算法分析与实现
前面一篇中我们分析了SHA256的原理,并且实现了该算法,在这一篇中我们将进一步分析SHA512并实现之. 1.SHA简述 尽管在前面的篇章中我们介绍过SHA算法,但出于阐述的完整性我依然要简单的说明 ...
- jquery easyui datagrid 加每页合计和总合计
jquery easyui datagrid 加每页合计和总合计 一:效果图 二:代码实现 这个只有从后台来处理 后台根据rows 和page两个参数返回的datatable 命名为dt 然后根据dt ...
- Confluence 6 workbox 包含从 Jira 来的通知
如果你的 Confluence 站点链接了一个 Jira 应用,你可以包含从 Jira 应用来的通知,例如 Jira 软化或 Jira 服务器桌面. 希望包含有从 Jira 应用来的通知: 你的 Ji ...
- linux和windows下,C/C++开发的延时函数,sleep函数
简介: 函数名: sleep 功 能: 执行挂起一段时间 用 法: unsigned sleep(unsigned seconds); 在VC中使用带上头文件 #include < ...
- poj3254 炮兵阵地弱化版,记数类dp
/* dp[i][j]表示到第i行的状态j有多少放置方式 */ #include<iostream> #include<cstring> #include<cstdio& ...
- CentOS下将Python的版本升级为3.x
本文主要介绍在Linux(CentOS)下将Python的版本升级为3.x的方法 众所周知,在2020年python官方将不再支持2.7版本的python,所以使用3.x版本的python是必要的,但 ...
- iOS 测试之非代码获取 iPhone 型号及其他信息
首先 安装libimobiledevice和ideviceinstaller $ brew uninstall ideviceinstaller $ brew uninstall libimobile ...
- spring cloud Hystrix监控面板Hystrix Dashboard和Turbine
我们提到断路器是根据一段时间窗内的请求情况来判断并操作断路器的打开和关闭状态的.而这些请求情况的指标信息都是HystrixCommand和HystrixObservableCommand实例在执行过程 ...
- 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正在读取软件包列表... 完成正 ...
- 该问题是需要导包!!!需要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 ...