每次循环向下寻找孩子时,随机选取一个孩子,设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. selenium 模拟滑动解锁

    来源:Selenium模拟JQuery滑动解锁   (selenium +Python ) 本文:selenium+Java package cn.gloryroad; import org.open ...

  2. php设计模式之适配器模式实例代码

    <?php header("Content-type:text/html;charset=utf-8"); // 适配器模式 /** * 查看天气接口 */ class Ti ...

  3. 【资源分享】Gmod日志记录脚本

    *----------------------------------------------[下载区]----------------------------------------------* ...

  4. java简单学生成绩管理系统

    题目要求: 一. 数据结构要求:(5 分) 1.定义 ScoreInformation 类,其中包括七个私有变量(stunumber, name, mathematicsscore, englishi ...

  5. [P3806] 【模板】点分治 - 点分治

    辣鸡蒟蒻怎么今天才来敲这个模板题 好像还敲了很久的样子 (大雾) #include <bits/stdc++.h> using namespace std; #define int lon ...

  6. oracle数据库操作 接口封装

    1.如下 using Oracle.ManagedDataAccess.Client; using System; using System.Collections.Generic; using Sy ...

  7. Install macOS High Sierra on Any Supported Intel-based PC

    1.准备制作安装盘前的准备https://www.tonymacx86.com/threads/unibeast-install-macos-high-sierra-on-any-supported- ...

  8. MySql -- primary key主键约束

    4.primary key主键约束 在一张表中: 1) 主键约束是 NOT NULL.UNIQUE唯一的记录. 2) 主键约束必须是唯一的值. 3) 主键约束列不能包含 NULL 值. 注意:每个表都 ...

  9. spring自动装配和通过java实现装配

    1.组建扫描 在类上添加注解@Component注解可以实现组建扫描 @Component public class A{ ... } 2.自动装配 通过在属性上或者方法上添加@Autowired注解 ...

  10. borderInterpolate()函数

    官网:borderInterpolate borderInterpolate 函数原型 int borderInterpolate( int p, int len, int borderType ); ...