Dudu's maze (DFS dyeing | disjoint-set)

To seek candies for Maomao, Dudu comes to a maze. There are nn rooms numbered from 11 to nn and mmundirected 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 aaand 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.

The only answer to this question does not meet the requirements of the answers are correct

Sample input

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

Sample Output

2.000000 
2.250000 

meaning of the questions: is to give a n points, drawing m edges, there may be heavy side, the figures are only two rooms, a kind of monster, the other is a candy shop, and people have a chance to display their magic , when faced with the monster, the monster can randomly go side room is connected to, people from the room 1,
it can get candy expectation is how much room is always 1 Hansel and Gretel.
We will become a candy store one block Unicom, the opportunity to use magic and China Unicom is certainly a monster block connecting rooms One room, then we can know all the candy in the room Unicom One block will get, before adding, then Unicom is adjacent blocks adjacent to each room in the number of monster candy summation (Unicom One block needs to be set 0),
then the number of sides of the room and ÷ connected.


 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn = 400005;
 5 int ind[maxn];
 6 int t;
 7 int n,m,k;
 8 struct Node
 9 {
10     int next,y;
11 } node[maxn];
12 int cnt,head[maxn];
13 
14 void add(int x,int y)
15 {
16     node[++cnt].y=y;
17     node[cnt].next=head[x];
18     head[x]=cnt;
19 }
20 
21 int moster[maxn],top;
22 bool moster_[maxn];
23 int vis[maxn];
24 int col_num[maxn];
25 
26 void dfs(int x,int col,int &cnt)
27 {
28     for(int i=head[x]; i; i=node[i].next)
29     {
30         int y = node[i].y;
31         if(!vis[y] && !moster_[y])
32         {
33             vis[y] = col;
34             dfs(y,col,++cnt);
35         }
36         else if(col == 1 && moster_[y] && !vis[y])
37         {
38             vis[y] = col;
39             moster[++top] = y;
40         }
41     }
42 }
43 int main()
44 {
45     scanf("%d",&t);
46     while(t--)
47     {
48         cnt = top = 0;
49         scanf("%d%d%d",&n,&m,&k);
50         for(int i=1;i<=n;i++)
51         {
52             moster_[i] = vis[i] = col_num[i] = ind[i] = head[i] = 0;
53         }
54         for(int i=1; i<=m; i++)
55         {
56             int u,v;
57             scanf("%d%d",&u,&v);
58             add(u,v);
59             add(v,u);
60             ind[v]++;
61             ind[u]++;
62         }
63         for(int i=1; i<=k; i++)
64         {
65             int x;
66             scanf("%d",&x);
67             moster_[x]=1;
68         }
69         int col = 0;
70         for(int i=1; i<=n; i++)
71         {
72             if(!vis[i] && !moster_[i])
73             {
74                 int cnt = 1;
75                 vis[i] = ++col;
76                 dfs(i,col,cnt);
77                 col_num[col] = cnt;
78             }
79         }
80         double init = col_num[1];
81         double ans = 0;
82         col_num[1] = 0;
83         for(int i=1; i<=top; i++)
84         {
85             double tmp = 0;
86             for(int j=head[moster[i]]; j; j=node[j].next)
87             {
88                 int y = node[j].y;
89                 tmp += col_num[vis[y]];
90             }
91             ans = max(ans,init+tmp/ind[moster[i]]);
92         }
93         printf("%.7f\n",ans);
94     }
95 }
View Code

 



Guess you like

Origin www.cnblogs.com/iwannabe/p/11521606.html