zoukankan      html  css  js  c++  java
  • CodeForces

    Discription

    Heidi is terrified by your estimate and she found it unrealistic that her friends would collaborate to drive her into debt. She expects that, actually, each person will just pick a random friend to send Heidi to. (This randomness assumption implies, however, that she can now visit the same friend an arbitrary number of times...) Moreover, if a person only has one friend in common with Heidi (i.e., if that person is in a leaf of the tree), then that person will not send Heidi back (so that Heidi's travel will end at some point).

    Heidi also found unrealistic the assumption that she can make all the travels in one day. Therefore now she assumes that every time she travels a route between two friends, she needs to buy a new ticket. She wants to know: how much should she expect to spend on the trips?

    For what it's worth, Heidi knows that Jenny has at least two friends.

    Input

    The first line contains the number of friends n (3 ≤ n ≤ 105). The next n - 1 lines each contain three space-separated integers uv and c (0 ≤ u, v ≤ n - 1, 1 ≤ c ≤ 104) meaning that u and v are friends and the cost for traveling between u and v is c(paid every time!).

    It is again guaranteed that the social network of the input forms a tree.

    Output

    Assume that the expected cost of the trips is written as an irreducible fractiona / b (that is, a and b are coprime). Then Heidi, the weird cow that she is, asks you to output . (Output a single integer between 0 and 109 + 6.)

    Examples

    Input
    3
    0 1 10
    0 2 20
    Output
    15
    Input
    4
    0 1 3
    0 2 9
    0 3 27
    Output
    13
    Input
    7
    0 1 3
    0 5 7
    1 2 2
    1 3 1
    1 4 5
    5 6 8
    Output
    400000019
    Input
    11
    1 0 6646
    2 0 8816
    3 2 9375
    4 2 5950
    5 1 8702
    6 2 2657
    7 2 885
    8 7 2660
    9 2 5369
    10 6 3798
    Output
    153869806
    Input
    6
    0 1 8
    0 2 24
    1 3 40
    1 4 16
    4 5 8
    Output
    39

    Note

    In the first example, with probability 1 / 2 Heidi will go to 1 from 0, and with probability 1 / 2 she will go to 2. In the first case the cost would be 10, and in the second it would be 20. After reaching 1 or 2 she will stop, as 1 and 2 are leaves of the social tree. Hence, the expected cost she has to pay is 10·1 / 2 + 20·1 / 2 = 15.

    In the third example, the expected cost is 81 / 5. You should output 400000019.

    In her travels, Heidi has learned an intriguing fact about the structure of her social network. She tells you the following: The mysterious determinant that you might be wondering about is such that it does not cause strange errors in your reasonable solution... Did we mention that Heidi is a weird cow?

       比较常规的 树上期望dp 消去后效性的题目。

        直接树上dp是会出事的,,因为还能往爸爸那里走。。。。

        但是我们可以设 f(x) = a * f(fa[x]) + b,这样的话把列的期望的式子拆开,就可以化简得 : f(x) = a * f(fa[x]) + c * f(x) [这一部分是从儿子们来的] + b。

        直接移项乘逆元就好啦

     

    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    const int maxn=1e5+5,ha=1e9+7;
    int ADD(int x,int y){ x+=y; return x>=ha?x-ha:x;}
    int n,m,hd[maxn],ne[maxn*2],to[maxn*2],num,val[maxn*2],F[maxn],G[maxn],D[maxn];
    inline void add(int x,int y,int z){ to[++num]=y,ne[num]=hd[x],hd[x]=num,val[num]=z;}
    inline int ksm(int x,int y){ int an=1; for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha; return an;}
    
    void dfs(int x,int fa){
    	if(fa>=0&&D[x]==1) return;
    	
    	G[x]=ksm(D[x],ha-2);
    	int tmp=1;
    	
    	for(int i=hd[x];i;i=ne[i]) if(to[i]!=fa){
    		dfs(to[i],x);
    		tmp=ADD(tmp,ha-G[x]*(ll)G[to[i]]%ha);
    		F[x]=ADD(F[x],G[x]*(ll)ADD(val[i],F[to[i]])%ha);
    	}
    	else F[x]=ADD(F[x],val[i]*(ll)G[x]%ha);
    	
    	tmp=ksm(tmp,ha-2);
    	F[x]=F[x]*(ll)tmp%ha,G[x]=G[x]*(ll)tmp%ha;
    }
    
    int main(){
    	scanf("%d",&n);
    	int uu,vv,ww;
    	for(int i=1;i<n;i++){
    	    scanf("%d%d%d",&uu,&vv,&ww);
    		add(uu,vv,ww),add(vv,uu,ww);
    		D[uu]++,D[vv]++;
    	}
    	dfs(0,-1);
    	printf("%d
    ",F[0]);
    	return 0;
    }
    

      

  • 相关阅读:
    新闻发布系统之 登陆注销
    readonly和const的区别
    c#中文件流的读写
    解决jsp两种提交方式乱码 的方法
    B/S 和 C/S两种架构
    App.config的典型应用
    接口实现多态
    Spring概念
    在Spring的核心配置文件applicationContext.xml中配置事务,主要配置三大方面:事务管理器、事务通知和定义事务性切面。
    乱码问题解决方案
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8992014.html
Copyright © 2011-2022 走看看