2021 ICPC济南站K题Search For Mafuyu_dfs

链接:PTA | 程序设计类实验辅助教学平台

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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值