Random Access Iterator

Recently Kumiko learns to use containers in C++ standard template library.

She likes to use the std::vector very much. It is very convenient for her to do operations like an ordinary array. However, she is concerned about the random-access iterator use in the std::vector. She misunderstanding its meaning as that a vector will return an element with equal probability in this container when she access some element in it.

As a result, she failed to solve the following problem. Can you help her?

You are given a tree consisting of nn vertices, and 11 is the root of this tree. You are asked to calculate the height of it.

The height of a tree is defined as the maximum number of vertices on a path from the root to a leaf.

Kumiko's code is like the following pseudo code.

She calls this function dfs(1, 1), and outputs the maximum value of depth array.

Obviously, her answer is not necessarily correct. Now, she hopes you analyze the result of her code.

Specifically, you need to tell Kumiko the probability that her code outputs the correct result.

To avoid precision problem, you need to output the answer modulo 10^9 + 7109+7.

Input

The first line contains an integer nn - the number of vertices in the tree (2 \le n \le 10^6)(2n106).

Each of the next n - 1n1 lines describes an edge of the tree. Edge ii is denoted by two integers u_iui and v_ivi, the indices of vertices it connects (1 \le u_i, v_i \le n, u_i \cancel= v_i)(1ui,vin,ui=vi).

It is guaranteed that the given edges form a tree.

Output

Print one integer denotes the answer.

样例输入复制
5
1 2
1 3
3 4
3 5
样例输出复制
750000006
样例解释

Kumiko's code has \frac{3}{4}43 probability to output the correct answer.

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 3e6 + 10;
const ll mod=1e9+7;

int n;
vector<int> e[maxn];
int dep[maxn], max_depth;
ll dp[maxn];

inline void init(int cur, int fa) {
    for (register int i = 0; i < e[cur].size(); ++i) {
        int to = e[cur][i];
        if(to==fa)continue;
        dep[to] = dep[cur] + 1;
        max_depth = max(max_depth, dep[to]);
        init(to, cur);
    }
}

inline ll fast_pow(ll a,ll b){
    ll res=1;
    while(b){
        if(b&1)res=(res*a)%mod;
        a=(a*a)%mod;
        b>>=1;
    }
    return res;
}

inline void solve(int cur, int fa) {
    bool flag = false;
    int tot=e[cur].size();
    if(cur!=1)--tot;
    int probability=fast_pow(tot,mod-2);
    int sum=0;
    for(register int i=0;i<e[cur].size();++i){
        int to=e[cur][i];
        if(to==fa)continue;
        solve(to,cur);
        sum=(sum+(1-dp[to]+mod)%mod*probability%mod)%mod;
        flag=true;
    }
    if(flag){
        dp[cur]=(1-fast_pow(sum,tot)+mod)%mod;
    }
    else{
        if(dep[cur]==max_depth) {
            dp[cur] = 1;
        }
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.txt","r",stdin);
#endif
    scanf("%d", &n);
    for (register int i = 1, u, v; i < n; ++i) {
        scanf("%d%d", &u, &v);
        e[u].emplace_back(v);
        e[v].emplace_back(u);
    }
    init(1, 1);
    //printf("debug max_depth = %d\n",max_depth);
    solve(1, 1);
    printf("%lld", dp[1]);
    return 0;
}

 

转载于:https://www.cnblogs.com/czy-power/p/11482677.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值