每次循环向下寻找孩子时,随机选取一个孩子,设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. day04_1hibernate

    log4j的整合.一对一关系的操作.二级缓存的介绍 一.log4j的整合: 1.介绍什么是 slf4j 核心jar  : slf4j-api-1.6.1.jar .slf4j是日志框架,将其他优秀的日 ...

  2. centos7安装启动firefox

    1.卸载系统之前Firefox sudo yum erase firefox 2.安装firefox命令: sudo yum install firefox 3.驱动下载地址: https://git ...

  3. Uncaught TypeError: Cannot read property 'addEventListener' of null

    <script type="text/javascript"> var body1=document.getElementById('#body') </scri ...

  4. MySQL5.7的参数优化

    https://www.cnblogs.com/zhjh256/p/9260636.html query_cache_size = 0query_cache_type=0innodb_undo_tab ...

  5. 051_switch语句的使用 052_while循环详解 053_for循环详解_dowhile简介 054_嵌套循环_循环相关练习

    051_switch语句的使用 package testmode2;/** * 测试switch语句 * 遇到多值判断的时候,使用switch.当然,switch完全可以使用ifelseifelse代 ...

  6. powerdesigner16.5改变数据模型字体大小

    1. 点击 2. 选择 3. 选择完点击确定后: 4. 点击设为默认:再点ok

  7. selenium的定位方法-单元素定位

    selenium自动化测试中,提供了单个元素定位方法,多个元素定位方法,2种方式都是根据元素属性:ID.NAME.CLASS_NAME.TAG_NAME.CSS_SELECTOR.XPATH.LINK ...

  8. ASCII编码,将英文存储到计算机

    前面我们已经讲到,计算机是以二进制的形式来存储数据的,它只认识 0 和 1 两个数字,我们在屏幕上看到的文字,在存储之前都被转换成了二进制(0和1序列),在显示时也要根据二进制找到对应的字符. 可想而 ...

  9. fdssd

    #include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #in ...

  10. intellij手动创建Mybatis遇到java.io.IOException: Could not find resource mybatis.xml

    将配置文件夹设置成resources即可