题目
思路
这道题由于是棵树,所以每条交通线都是相对独立的,所以易得总期望就是 n − 1 2 + 1 = n + 1 2 \frac{n-1}{2}+1=\frac{n+1}{2} 2n−1+1=2n+1
代码
#include<iostream>
#include<cstdio>
using namespace std;
const long long mod=1e9+7;
long long n,x,y;
long long poww(long long x,long long y)
{
long long js=1;
while(y)
{
if(y&1)
js=(js*x)%mod;
y>>=1;
x=(x*x)%mod;
}
return js%mod;
}
int main()
{
scanf("%lld",&n);
for(int i=1; i<=n-1; i++)
scanf("%lld%lld",&x,&y);
printf("%lld",(n+1)*poww(2,n-2)%mod);
return 0;
}