ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Dudu's maze

2019-09-14 23:55:37  阅读:245  来源: 互联网

标签:le room int cur maxn maze Dudu he


  •  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 51≤t≤5

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 1000001≤n≤100000, n-1 \le m \le 2*nn−1≤m≤2∗n, 0 \le k \le n0≤k≤n

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 n1≤a,b≤n

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 k1≤i≤k

Output

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

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

样例输入

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

 

标签:le,room,int,cur,maxn,maze,Dudu,he
来源: https://www.cnblogs.com/czy-power/p/11520782.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有