-
题意:给你一颗树(边是无向的),从根节点向下走,统计走到每个子节点的概率,求所有叶子节点的深度乘上概率的和.
-
题解:每层子节点的概率等于上一层节点的概率乘\(1\)除以这层的子节点数,所以我们用\(dfs\)或者\(bfs\)都可以写,其实就是个搜索裸题,注意给出边是无向的就好了.
-
代码:
1.dfs:
int n; int a,b; vector<int> v[N]; double ans; void dfs(int u,int fa,double p,int len){ if((int)v[u].size()==1 && u!=1){ ans+=p*len;len; return; } for(auto w:v[u]){ double p1; if(w==fa) continue; if(u==1) p1=p*(1.0/(double)(v[u].size())); else p1=p*(1.0/(double)(v[u].size()-1)); dfs(w,u,p1,len+1); } } int main() { //ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); scanf("%d",&n); for(int i=1;i<n;++i){ scanf("%d %d",&a,&b); v[a].pb(b); v[b].pb(a); } dfs(1,-1,1.0,0); printf("%.6f\n",ans); return 0; }
2.bfs
struct misaka{ int node; int fa; double p; double len; }e; int n; int a,b; vector<int> v[N]; double ans; void bfs(){ queue<misaka> q; e.node=1;e.p=1.0;e.len=0.0;e.fa=-1; q.push(e); while(!q.empty()){ auto tmp=q.front(); q.pop(); int node=tmp.node; double p=tmp.p; double len=tmp.len; int fa=tmp.fa; if((int)v[node].size()==1 && node!=1){ ans+=p*len; } for(auto w:v[node]){ if(w==fa) continue; e.node=w; if(node==1) e.p=p*((double)1.0/(double)v[node].size()); else e.p=p*(1.0/(double)(v[node].size()-1)); e.len=len+1.0; e.fa=node; q.push(e); } } } int main() { ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); cin>>n; e.len=1.0; e.p=1.0; for(int i=1;i<n;++i){ cin>>a>>b; v[a].pb(b); v[b].pb(a); } bfs(); printf("%.6f\n",ans); return 0; }
相关文章
- 10-27Codeforces Round #524 (Div. 2) C. Masha and two friends
- 10-27Codeforces Round #394 (Div. 2) C. Dasha and Password 暴力
- 10-2701背包 Codeforces Round #267 (Div. 2) C. George and Job
- 10-27C. Moamen and XOR[Codeforces Round #737 (Div. 2)]
- 10-27Codeforces Round #379 (Div. 2) C. Anton and Making Potions 二分
- 10-27Codeforces Round #379 (Div. 2) C. Anton and Making Potions —— 二分
- 10-27Codeforces Round #381 (Div. 2)C. Alyona and mex(思维)
- 10-27Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s
- 10-27Codeforces Round #692 (Div. 2)C. Peaceful Rooks(并查集求环数)
- 10-27Codeforces Round #692 (Div. 2) C. Peaceful Rooks(图论,并查集)