Codeforces Round #428 (Div. 2) C. Journey

There are n cities and n?-?1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads.

Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren‘t before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities.

Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link https://en.wikipedia.org/wiki/Expected_value.

Input

The first line contains a single integer n (1?≤?n?≤?100000) — number of cities.

Then n?-?1 lines follow. The i-th line of these lines contains two integers ui and vi (1?≤?ui,?vi?≤?n, ui?≠?vi) — the cities connected by the i-th road.

It is guaranteed that one can reach any city from any other by the roads.

Output

Print a number — the expected length of their journey. The journey starts in the city 1.

Your answer will be considered correct if its absolute or relative error does not exceed 10?-?6.

Namely: let‘s assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

Examples

Input

41 21 32 4

Output

1.500000000000000

Input

51 21 33 42 5

Output

2.000000000000000

Note

In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5.

In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2.

求数学期望,dfs做,因为n个点只有n-1个边,所以就没有连成圈,len的长度其实就时与1相连的个数,然后用ans来保持最底层第的深度和  答案就时ans/len了。

比如第二组数据,与1相连的点有两个,所以len是2,最底层第分别时4和5,深度分别2,2,所以答案就时(2+2)/len。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 100010;
 4 vector<int> G[N];
 5
 6 double dfs(int v, int p) {
 7     double ans = 0;
 8     int len = 0;
 9     for(int i = 0; i < G[v].size(); i ++) {
10         int u = G[v][i];
11         if(u == p) continue;
12         ans += (1 + dfs(u, v));
13         len ++;
14     }
15     return len == 0 ? 0.0 : ans / len;
16 }
17 int main() {
18     int n;
19     scanf("%d", &n);
20     for(int i = 1; i < n; i ++) {
21         int u, v;
22         scanf("%d %d", &u, &v);
23         G[u].push_back(v);
24         G[v].push_back(u);
25     }
26     printf("%.15f\n",dfs(1,0));
27     return 0;
28 }
时间: 2024-10-15 10:36:05

Codeforces Round #428 (Div. 2) C. Journey的相关文章

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #428 (Div. 2) D. Winter is here 数学

链接: http://codeforces.com/contest/839/problem/D 题意: 给出一些数,求取出一些数,当他们的GCD大于0时,将数量乘GCD累加到答案上,求累加和. 题解: 这道题比赛的时候忘了考虑重复了,wa掉之后发现可能会出现重复,然而又不会写了.. 这道题需要知道一个公式就是 1*C(n,1)+2*C(n,2)+3*C(n,3)+...+n*C(n,n) = n*2^(n-1) 我们枚举GCD,统计为其倍数的数字数量,先假定其能组成的集合数为贡献, 但是我们发现

Codeforces Round #428 (Div. 2) A-C

A. Arya and Bran 水题 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <iomanip> #include <math.h> #include <map> using namespace

【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (DP)

C. Journey time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city, numbered

Codeforces Round #428 (Div. 2) D. Winter is here[数论 II]

题目:http://codeforces.com/contest/839/problem/D 题意:找出每种情况使得所有数字gcd不为1,对答案的贡献为gcd值乘数字个数. 题解:因为数字不大,可以哈希出每种数字的个数,然后从后往前,f[i]代表在gcd==i时存在的数字搭配种数.每次计算i时,要减去计算过的种数,所以从后向前计算.每个数字贡献次数为${2}^{sum-1}$(写二进制数的情况可以观察出来),所有数字贡献为$sum*{2}^{sum-1}$ 2次x次方可以先预处理取模出来. 1

Codeforces Round #428 (Div. 2) D. Winter is here[数论II][容斥原理]

传送门:http://codeforces.com/contest/839/problem/D Examples input 33 3 1 output 12 input 42 3 4 6 output 39 Note In the first sample the clans are {1},?{2},?{1,?2} so the answer will be 1·3?+?1·3?+?2·3?=?12 题解:当有n个数为x的倍数时 gcd为x对答案的贡献为$1*C_n^1+2*C_n^2+..

Codeforces Round #374 (Div. 2)-C. Journey DP

C. Journey 题意: 在一个DAG(有向无环图)中,问从1 到 n 点,在时间限制K下,最多能游玩几个地点,把游玩的顺序顺便输出. 思路: 感觉dp,一维不够就加一维,我一开始有想到dp,但是只是一维的去推,推着感觉不正确.这次用dp[i][j],表示到j点已游玩i个地点的最少时间. DAG中一般要想到求拓扑序.即保证dp从左到右. 这题这个人直接两重循环求dp,不知道为啥也是对的 ,确实是有道理,因为dp[ i - 1]这一行的所有情况对于 i 行都是确定的,且i行一定是在i-1行存在

【容斥原理】Codeforces Round #428 (Div. 2) D. Winter is here

给你一个序列,让你对于所有gcd不为1的子序列,计算它们的gcd*其元素个数之和. 设sum(i)为i的倍数的数的个数,可以通过容斥算出来. 具体看这个吧:http://blog.csdn.net/jaihk662/article/details/77161436. 注意1*C(n,1)+2*C(n,2)+...+n*C(n,n)=n*2^(n-1). #include<cstdio> using namespace std; typedef long long ll; #define MOD

Codeforces Round #374 (Div. 2) C. Journey

题解: dfs+dp dp[i][j]表示经过i个点后到达j点的最小花费 在dfs每个点的过程中加个for循环i 注意用 long long MLE 代码: #include<bits/stdc++.h> #define maxn 5010 #define mod 1000000007 #define ll long long #define pb push_back using namespace std; const int INF=1e9+7; struct Edge { int v,n