cf839c Journey

大水题

#include <iostream>
#include <cstdio>
using namespace std;
int n, du[100005], hea[100005], cnt, uu, vv;
double dp[100005];
struct Edge{
	int too, nxt;
}edge[200005];
void add_edge(int fro, int too){
	edge[++cnt].nxt = hea[fro];
	edge[cnt].too = too;
	hea[fro] = cnt;
}
void dfs(int x, int f){
	for(int i=hea[x]; i; i=edge[i].nxt){
		int t=edge[i].too;
		if(t!=f){
			dfs(t, x);
			dp[x] += (dp[t] + 1.0) / du[x];
		}
	}
}
int main(){
	cin>>n;
	for(int i=1; i<n; i++){
		scanf("%d %d", &uu, &vv);
		add_edge(uu, vv);
		add_edge(vv, uu);
		du[uu]++;
		du[vv]++;
	}
	for(int i=2; i<=n; i++)
		du[i]--;
	dfs(1, 0);
	printf("%lf", dp[1]);
	return 0;
}
原文地址:https://www.cnblogs.com/poorpool/p/8330631.html