题解:P15446 「IXOI R1」BA BA 博弈
论题目名应该怎么读。
题意
给定一棵以
Solution
首先到达每个点的概率是好算的。
考虑维护当前可能到达的节点集合
当
当
当
对于到达每个
视实现复杂度为
::::success[Code]
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
const int N=1e6+5;
const ll mod=1e9+7;
int n,f[N];
ll inv[N],ans,p[N];
bool ck[N],lf[N];
vector<int>e[N];
ll Pow(ll x,ll y){
ll res(1);
while(y){
if(y&1)(res*=x)%=mod;
(x*=x)%=mod;
y>>=1;
}
return res;
}
void init(int u,int fa){
if(u!=1&&e[u].size()==1)ck[fa]=lf[u]=1;
f[u]=fa;
for(auto v : e[u])
if(v!=fa)init(v,u);
}
void find(int u){
set<int>S;
S.insert(u);
while(!S.empty()){
set<int>T;
int tot(0);
ll o(0);
for(auto x : S){
int dg=(x==1)+e[x].size()-1;
for(auto v : e[x])
if(v!=f[x]){
p[v]=p[x]*inv[dg]%mod;
if(lf[v])continue;
else if(ck[v])++tot;
(o+=p[v])%=mod;
T.insert(v);
}
}
S=T;
if(tot>1){
(ans+=o)%=mod;
for(auto v : S)
find(v);
return;
}
}
}
int main(){
scanf("%d",&n);
for(int i=1,u,v;i<n;++i){
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
for(int i=1;i<=n;++i)
inv[i]=Pow(i,mod-2);
init(1,0);
p[1]=1;
find(1);
printf("%lld",ans);
return 0;
}
::::