Mafuyu has hidden in Sekai, and Kanade is searching for her.
In Sekai, there is nothing but a lot of rooms. There are n rooms in Sekai, numbered from 1 to n. Besides, n−1 pairs of rooms are directly connected by corridors, such that it is possible to move from one room to any other one, using one or more corridors. In other words, rooms in Sekai form a tree.
Kanade is at room 1, and she knows that Mafuyu may hide in any room except room 1, with uniform probability. In one second, Kanade can move to a room adjacent to the room she is currently in. Once Kanade is in the same room with Mafuyu, she immediately finds her. What is the minimum expected time for Kanade to find Mafuyu, if Kanade is taking the optimal strategy?
Input
The first line contains an integer t (1≤t≤1000) --- the number of test cases.
The first line in each test case contains an integer n (2≤n≤100) --- the number of rooms.
Each of the following n−1 lines contains two integers ai, bi (1≤ai,bi≤n) --- the rooms connected by the i-th corridor. It is guaranteed that it is possible to move from one room to any other one, using one or more corridors.
Output
Output t real numbers. For each test case, output the minimum expected time. Your answer is considered correct if the absolute or relative error is less than 10−9.
Sample Input
4
2
1 2
5
1 2
2 3
3 4
1 5
7
1 2
1 3
2 4
2 5
3 6
3 7
10
1 2
2 3
3 4
1 5
5 6
6 7
1 8
8 9
9 10
Output
1.0000000000
3.2500000000
5.3333333333
8.0000000000
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
int n;
int cnt = 0, ans = 0;
int vis[128] = {0};
void dfs(vector<int>g[], int cur)
{
cnt++;
// printf("cur = %d, cnt = %d\n", cur, cnt);
vis[cur] = 1;
ans += cnt;
for(int i = 0; i < g[cur].size(); i++)
{
if(!vis[g[cur][i]])
{
dfs(g, g[cur][i]);
cnt++;
}
}
}
int main()
{
// system("chcp 65001");
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("C:/Users/zhaochen/Desktop/input.txt", "r", stdin);
int T;
cin >> T;
while(T--)
{
cin >> n;
memset(vis, 0, sizeof(vis));
vector<int>g[128];
cnt = -1;
ans = 0;
for(int i = 0; i < n - 1; i++)
{
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(g, 1);
double x = (double)ans / (n - 1);
printf("%.10f\n", x);
}
return 0;
}