Random Access Iterator

\[Time Limit: 4000 ms \quad Memory Limit: 262144 kB
\]

题意

给出伪代码,问按着伪代码在树上跑,能够正确求出来树的深度的概率。

思路

先在树上 \(dfs\) 一遍,求出每个点可以走到的最深深度,用 \(deep\) 表示。

令 \(dp[i]\) 表示从 \(i\) 节点能够正确求出最深深度的概率。

  1. 如果 \(deep[u]\) 不等于最深深度,那么明显 \(dp[u]=0\)
  2. 如果 \(deep[u]\) 是最深深度而且走到了叶子节点,那么 \(dp[u]=1\)
  3. 其他情况,能够走到最深深度的概率 = \(1\) - 每次走每个点都不能走到最深深度的概率,用表达式表达就是,\(sz\) 表示当前节点可以走的其他节点数,特别注意 \(u=1\) 的情况就可以了。

\[ dp[u] = 1-\left(\frac{\sum_{v}(1-dp[v)}{sz-1}\right)^{sz-1}
\]

/***************************************************************
> File Name : a.cpp
> Author : Jiaaaaaaaqi
> Created Time : Wed 11 Sep 2019 02:48:22 PM CST
***************************************************************/ #include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int> typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e6 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std; int n, m;
int cas, tol, T; int maxdeep;
vector<int> vv[maxn];
int deep[maxn];
ll dp[maxn]; ll fpow(ll a, ll b) {
ll ans = 1;
while(b) {
if(b&1) ans = ans*a%mod;
a = a*a%mod;
b >>= 1;
}
return ans;
} void dfs(int u, int fa, int d) {
deep[u] = d;
for(auto v : vv[u]) {
if(v == fa) continue;
dfs(v, u, d+1);
deep[u] = max(deep[u], deep[v]);
}
maxdeep = max(maxdeep, deep[u]);
} void solve(int u, int fa) {
if(deep[u] != maxdeep) {
dp[u] = 0;
return ;
}
if(vv[u].size() == 1) {
dp[u] = 1;
return ;
}
int sz = vv[u].size()-1;
if(u == 1) sz++;
ll ans = 0;
for(auto v : vv[u]) {
if(v == fa) continue;
solve(v, u);
ans += (1-dp[v]+mod);
ans %= mod;
}
ans = ans*fpow(sz, mod-2)%mod;
ans = fpow(ans, sz);
dp[u] = (1-ans+mod)%mod;
} int main() {
// freopen("in", "r", stdin);
scanf("%d", &n);
for(int i=1; i<=n; i++) {
vv[i].clear();
}
for(int i=1; i<n; i++) {
int u, v;
scanf("%d%d", &u, &v);
vv[u].pb(v);
vv[v].pb(u);
}
maxdeep = 0;
dfs(1, 0, 1);
solve(1, 0);
printf("%lld\n", dp[1]);
return 0;
}

Random Access Iterator 徐州网络赛(树形dp)的更多相关文章

  1. HDU 6201 2017沈阳网络赛 树形DP或者SPFA最长路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6201 题意:给出一棵树,每个点有一个权值,代表商品的售价,树上每一条边上也有一个权值,代表从这条边经过 ...

  2. hdu 4274 2012长春赛区网络赛 树形dp ***

    设定每个节点的上限和下限,之后向上更新,判断是否出现矛盾 #include<cstdio> #include<iostream> #include<algorithm&g ...

  3. ICPC 2019 徐州网络赛

    ICPC 2019 徐州网络赛 比赛时间:2019.9.7 比赛链接:The Preliminary Contest for ICPC Asia Xuzhou 2019 赛后的经验总结 // 比赛完才 ...

  4. 2019徐州网络赛 I J M

    I. query 比赛时候没有预处理因子疯狂t,其实预处理出来因子是\(O(nlog(n))\)级别的 每个数和他的因子是一对偏序关系,因此询问转化为(l,r)区间每个数的因子在区间(l,r)的个数 ...

  5. 2018 ICPC 徐州网络赛

    2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution ...

  6. 2019icpc徐州网络赛

    A Who is better? 题意 excrt+斐波那契博弈 分析 Java的BigInteger对象默认为null,不能直接比较. 代码 import java.math.BigInteger; ...

  7. Random Access Iterator

    Random Access Iterator 树型概率DP dp[u]代表以当前点作为根得到正确结果的概率 将深度最深的几个点dp[u]很明显是1 然后很简单的转移 有k次,但我们要先看一次的情况,然 ...

  8. 计蒜客 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 ...

  9. [徐州网络赛]Longest subsequence

    [徐州网络赛]Longest subsequence 可以分成两个部分,前面相同,然后下一个字符比对应位置上的大. 枚举这个位置 用序列自动机进行s字符串的下标转移 注意最后一个字符 #include ...

随机推荐

  1. Golang(九)简单 Goroutine 池实现

    0. 前言 最近使用 Golang 写一个并发执行的测试脚本 之前习惯使用 Java,习惯性想先建一个线程池.然后意识到 Golang 没有封装好的线程池 结合之前学习的 Goroutine 原理和 ...

  2. nginx学习笔记2

    nginx基础配置 一.nginx常用命令 nginx -s reload:在nginx已经启动的情况下重新加载配置文件(平滑重启) nginx -s reopen:重新打开日志文件 nginx -c ...

  3. rdd 基本操作

    package com.jason.example import org.apache.spark.rdd.RDD class RddTest extends SparkInstance { val ...

  4. Jenkins打包编码GBK的不可映射字符

    1.错误信息如下: ​ 2.在Maven的POM中加入如下代码,然后重新打包即可. <properties> <!-- 文件拷贝时的编码 --> <project.bui ...

  5. Centos7.5 安装Mysql5.7

    #yum -y install wget #wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rp ...

  6. C++中的双冒号作用

    1. 作用域符号::的前面一般是类名称,后面一般是该类的成员名称,C++为例避免不同的类有名称相同的成员而采用作用域的方式进行区分如:A,B表示两个类,在A,B中都有成员member.那么      ...

  7. CAS5单点登录

    看这篇文章即可:https://www.jianshu.com/p/c1273d81c4e4>https://www.jianshu.com/p/c1273d81c4e4

  8. 【BZOJ4487】[JSOI2015]染色问题(容斥)

    [BZOJ4487][JSOI2015]染色问题(容斥) 题面 BZOJ 题解 看起来是一个比较显然的题目? 首先枚举一下至少有多少种颜色没有被用到过,然后考虑用至多\(k\)种颜色染色的方案数. 那 ...

  9. 前端学习:JS(面向对象)代码笔记

    前端学习:JS(面向对象)代码笔记 前端学习:JS面向对象知识学习(图解) 创建类和对象 创建对象方式1调用Object函数 <body> </body> <script ...

  10. wcf序列化嵌套类(如TreeNode)异常原因

    循环引用类在WCF中的传递 循环引用类在WCF中的传递问题,例如: [DataContract]    public class AB    {        public string name { ...