每次循环向下寻找孩子时,随机选取一个孩子,设dp[u]为从u出发,不能得出正确答案的概率,则从u出发,走一次的情况下不能得出正确答案的概率是 P = (dp[v1]+dp[v2]+dp[v3]+……dp[vk]) / cnt_son[u] ,则从u出发,要走cnt_son[u]次,那么dp[u]=P^cnt_con[u]

dp的意义也可以改成能得出正确答案的概率,下面的式子稍微改改就行了

为了避免除法的精度问题,num/k %mod,它等于 num * ni %mod ,ni等于k在模mod意义下的逆元。

#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
const int maxn=1e6+10; long long N;
vector<long long> vc[maxn];
long long deep[maxn],dp[maxn],MaxDeep=-1,son[maxn]; void dfs(long long u,long long pre)
{
deep[u]=deep[pre]+1;
MaxDeep=max(MaxDeep,deep[u]);
int size=vc[u].size();
for (int i=0;i<size;i++) {
if (vc[u][i]!=pre) {
son[u]++;
dfs(vc[u][i],u);
}
}
} long long quick_pow(long long base,long long p)
{
long long ans=1;
while (p) {
if (p&1) {
ans=ans*base%mod;
}
base=base*base%mod;
p>>=1;
}
return ans;
} void dfs2(long long u,long long pre)
{
if (!son[u]) {
if (deep[u]==MaxDeep) {
dp[u]=0;
}
else {
dp[u]=1;
}
return ;
}
int size=vc[u].size();
long long tmp=0,ni=quick_pow(son[u],mod-2);
for (int i=0;i<size;i++) {
if (vc[u][i]==pre) continue;
dfs2(vc[u][i],u);
tmp=(tmp+(dp[vc[u][i]]*ni)%mod)%mod;
}
dp[u]=quick_pow(tmp,son[u]);
} int main()
{
scanf("%lld",&N);
long long u,v;
for (int i=1;i<N;i++) {
scanf("%lld%lld",&u,&v);
vc[u].push_back(v);
vc[v].push_back(u);
}
dfs(1,0);
dfs2(1,0);
printf("%lld\n",(1-dp[1]+mod)%mod);
// for (int i=0;i<=N;i++) {
// printf("%lld %lld\n",deep[i],son[i]);
// }
// printf("%lld\n",MaxDeep);
return 0;
}

The Preliminary Contest for ICPC Asia Xuzhou 2019 J Random Access Iterator (树形DP)的更多相关文章

  1. 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)

    query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...

  2. The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team [单调栈上二分]

    也许更好的阅读体验 \(\mathcal{Description}\) 给n个数,与一个数m,求\(a_i\)右边最后一个至少比\(a_i\)大\(m\)的数与这个数之间有多少个数 \(2\leq n ...

  3. The Preliminary Contest for ICPC Asia Xuzhou 2019

    A:Who is better? 题目链接:https://nanti.jisuanke.com/t/41383 题意: 类似于有N个石子,先手第一次不能拿完,每次后手只能拿 1 到 前一次拿的数量* ...

  4. The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team

    题目链接:https://nanti.jisuanke.com/t/41387 思路:我们需要从后往前维护一个递增的序列. 因为:我们要的是wi + m <= wj,j要取最大,即离i最远的那个 ...

  5. 计蒜客 41387.XKC's basketball team-线段树(区间查找大于等于x的最靠右的位置) (The Preliminary Contest for ICPC Asia Xuzhou 2019 E.) 2019年徐州网络赛

    XKC's basketball team XKC , the captain of the basketball team , is directing a train of nn team mem ...

  6. The Preliminary Contest for ICPC Asia Xuzhou 2019 【 题目:so easy】{并查集维护一个数的下一个没有被删掉的数} 补题ING

    题意:给[1,n],n个数,有两种操作: 1 x,删去x2 x,查询还未被删去的数中大于等于x的最小的数是多少. input: output: 做法:按照并查集的方法压缩路径 代码: #include ...

  7. G.Colorful String(The Preliminary Contest for ICPC Asia Xuzhou 2019)

    https://nanti.jisuanke.com/t/4 #include <bits/stdc++.h> using namespace std; ,; typedef unsign ...

  8. E.XKC's basketball team(The Preliminary Contest for ICPC Asia Xuzhou 2019)

    https://nanti.jisuanke.com/t/41387 解: 离散化+线段树. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); ...

  9. A.Who is better?(The Preliminary Contest for ICPC Asia Xuzhou 2019)

    https://nanti.jisuanke.com/t/41383 解: 斐波那契博弈+中国剩余定理. #include <bits/stdc++.h> using namespace ...

随机推荐

  1. 电脑进不去BIOS解决办法

    把所有外设(主要是硬盘,包括装在主板上的固态硬盘)拆下来,拆下纽扣电池给主板放电,装回纽扣电池,重启F1进入BIOS. 最终查到原因,是固态那里出的问题,固态作为启动硬盘,被自己搞得有问题了,有两个启 ...

  2. 剑指offer系列——59/60.按之字形顺序打印二叉树/把二叉树打印成多行

    Q:请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. A:BFS,偶数层reverse vector&l ...

  3. jQuery尺寸

    jQuery 尺寸 jQuery width() 和 height() 方法 width() 方法设置或返回元素的宽度(不包括内边距.边框或外边距). height() 方法设置或返回元素的高度(不包 ...

  4. 【Python requests多页面爬取案例】

    "```python import requests from fake_useragent import UserAgent # 随机ua库 class Boring(): def __i ...

  5. Mike and Foam(位运算)

    English reading: bartender == barmaid:酒吧女招待 milliliter:毫升:千分之一毫升 foam:泡沫 a glass of beer with a good ...

  6. sql查询 —— 分组

    -- 分组 -- group by -- 分组只有与聚合函数一起使用才能发挥作用 -- 分组只限于字段分明 例如 性别 ,部门, --列出所有性别 select gender from student ...

  7. session的到底是做什么的?

    原文地址:https://blog.csdn.net/h19910518/article/details/79348051 前言: 今天就来彻底的学一些session是个啥东西,我罗列了几个需要知道的 ...

  8. mongo shell远程连接使用数据库

    mongo mydb --username user1 --host --password --username 用户名 --host 连接ip --port 连接端口号 --password 密码 ...

  9. ASP学习笔记1

    一.变量 1.1 声明变量 dim name name="Donald Duck" response.write("My name is: " & na ...

  10. python 字符串的一些函数

    split()函数 split()      以 空格 为分割符分割字符串,返回列表 split('_')   以'_'为分割符分割字符串,返回列表 strip() 函数  去掉前后的空格 下面是字符 ...