Dudu's maze

  •  23.7%
  •  1000ms
  •  262144K
 

To seek candies for Maomao, Dudu comes to a maze. There are nn rooms numbered from 11 to nn and mm undirected roads.

There are two kinds of rooms in the maze -- candy room and monster room. There is one candy in each candy room, and candy room is safe. Dudu can take the only candy away when entering the room. After he took the candy, this candy room will be empty. A empty room is also safe. If Dudu is in safe, he can choose any one of adjacent rooms to go, whatever it is. Two rooms are adjacent means that at least one road connects the two rooms.

In another kind of rooms, there are fierce monsters. Dudu can't beat these monsters, but he has a magic portal. The portal can show him a randomly chosen road which connects the current room and the other room.

The chosen road is in the map so Dudu know where it leads to. Dudu can leave along the way to the other room, and those monsters will not follow him. He can only use the portal once because the magic energy is not enough.

Dudu can leave the maze whenever he wants. That's to say, if he enters a monster room but he doesn't have enough energy to use the magic portal, he will choose to leave the maze immediately so that he can save the candies he have. If he leave the maze, the maze will never let him in again. If he try to fight with the monsters, he will be thrown out of the maze (never let in, of course). He remembers the map of the maze, and he is a clever guy who can move wisely to maximum the expection of candies he collected.

Maomao wants to know the expected value of candies Dudu will bring back. Please tell her the answer. He will start his adventure in room 1, and the room 1 is always a candy room. Since there may be more than one road connect the current room and the room he wants to go to, he can choose any of the roads.

Input

First line a integer tt, means tt cases. 1 \le t \le 51t5

For each case:

First line 3 integer nn, mm, kk, nn means the number of rooms, mm means the number of roads, kk means the number of monster rooms. 1 \le n \le 1000001n100000, n-1 \le m \le 2*nn1m2n, 0 \le k \le n0kn

Next mm lines, for each line there are two integer aa and bb, separated by a space, means there is a road between aa and bb. There may be repeated edges, but won't be self loop. 1 \le a,b \le n1a,bn

In the last line there are kk distinct numbers, the ii-th number x_ixi means the number of the ii-th monster room is x_ixi, and room 1 won't be monster room. 1 \le i \le k1ik

Output

For each case output a real number. The absolute error of the answer should not exceed 10^{-6}106.

本题答案不唯一,符合要求的答案均正确

样例输入复制
2
7 6 2
1 2
1 3
2 5
2 4
3 6
4 7
2 3
7 7 2
1 2
1 3
2 5
2 4
3 6
4 7
2 4
2 3
样例输出复制
2.000000
2.250000
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int maxn = 3e5;

int T, n, m, k;
struct node {
    int to, nx;
} o[maxn];
int tot, head[maxn], is[maxn], vis[maxn], cnt, re[maxn], fa[maxn], e[maxn], use[maxn];
int deg[maxn];
double res = 1.0;

inline void init() {
    tot = 0;
    cnt = 0;
    res = 1.0;
    memset(head, 0, sizeof(head));
    memset(vis, 0, sizeof(vis));
    for (register int i = 1; i <= n; ++i) {
        fa[i] = i, e[i] = 1, deg[i] = 0;
        is[i] = 0;
        use[i] = 0;
    }
}

inline void add_edge(int u, int v) {
    o[++tot].to = v;
    o[tot].nx = head[u];
    head[u] = tot;
}

inline void bfs() {
    queue<int> q;
    q.push(1);
    vis[1] = 1;
    while (!q.empty()) {
        int cur = q.front();
        q.pop();
        for (register int i = head[cur]; i; i = o[i].nx) {
            int to = o[i].to;
            if (vis[to])continue;
            if (is[to]) {
                re[++cnt] = to;
                vis[to] = 1;
                continue;
            }
            q.push(to);
            vis[to] = 1;
            ++res;
        }
    }
}

inline int find_fa(int x) {
    return x == fa[x] ? x : fa[x] = find_fa(fa[x]);
}

inline void dfs(int cur) {
    if (use[cur])return;
    use[cur] = 1;
    for (register int i = head[cur]; i; i = o[i].nx) {
        int to = o[i].to;
        if (vis[to] || is[to] || use[to])continue;

        //vis[to] = 1;
        dfs(to);
        use[to] = 1;
        int f_cur = find_fa(cur);
        int f_to = find_fa(to);
        if (f_cur != f_to) {
            fa[f_to] = f_cur;
            e[f_cur] += e[f_to];
        }
    }
}


int main() {
#ifndef INLINE_JUDGE
    //freopen("1.txt", "r", stdin);
#endif
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d%d", &n, &m, &k);
        init();
        for (register int i = 1, u, v; i <= m; ++i) {
            scanf("%d%d", &u, &v);
            add_edge(u, v);
            add_edge(v, u);
            ++deg[u];
            ++deg[v];
        }
        for (register int i = 1, x; i <= k; ++i) {
            scanf("%d", &x);
            is[x] = 1;
        }
        bfs();
        double mx = 0;
        for (register int i = 1; i <= cnt; ++i) {
            int cur = re[i];
            double pre = 0;
            for (register int j = head[cur]; j; j = o[j].nx) {
                int to = o[j].to;
                if (vis[to] || is[to])continue;
                dfs(to);
                pre += e[find_fa(to)];
            }
            mx = max(mx, pre / (double) deg[cur]);
        }
        printf("%f\n", res + mx);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/czy-power/p/11520782.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值