-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGRAFFDEF - King Graffs Defense.cpp
87 lines (76 loc) · 2.32 KB
/
GRAFFDEF - King Graffs Defense.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// https://cp-algorithms.com/graph/bridge-searching.html
// https://www.spoj.com/problems/GRAFFDEF/
#include <iostream>
#include <vector>
#include <iomanip> // for setprecision
#include <cstring> //for memset
#include <map>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
vector<int> graph[N];
int tin[N]; // first discovery time of a node u
int low[N]; // lowest time to reach from root to a node using a back or tree edge, initially it will be tin[u]
int tim = 0;
bool visited[N];
map<pair<int, int>, bool > bridge;
ll id = 0, count[N];
void findBridge(int u, int p){
visited[u] = true;
tim++;
tin[u] = low[u] = tim;
for(auto v: graph[u]){ // p -> u -> v
if(v == p) continue; // tree edges(p->u) are already calculated with dfs(by time++)
//child is already visited, use its discovery time(back edge)
//??: why are we using min(low[u], tin[v]) instead of min(low[u], low[v])
if(visited[v]){
low[u] = min(low[u], tin[v]);
}
//child not visited, do a dfs and use childs lowest time
else{
findBridge(v, u);
low[u] = min(low[u], low[v]);
// if lowest time to reach from root to a child is greater than that of parent
// it indicates there is no other way to reach to child without using par->child edge
if(low[v] > low[u]){
bridge[{u, v}] = bridge[{v, u}] = true;
}
}
}
}
void dfs(int u, int p){
visited[u] = true;
count[id]++;
for(auto v: graph[u]){
if(!visited[v] && !bridge[{u, v}])
dfs(v, u);
}
}
int main(){
ll n, m;
cin >> n >> m;
while(m--){
int x, y;
cin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
for(int i = 1; i <= n; i++){
if(!visited[i])
findBridge(i, 0);
}
memset(visited, 0, sizeof(visited));
for(int i = 1; i <= n; i++){
if(!visited[i]){
id++;
dfs(i, 0);
}
}
ll fail = 0;
for(int i = 1; i <= id; i++){
if(count[i]){
fail += (ll)(count[i]*(count[i]-1))/2;
}
}
cout << fixed << setprecision(5) << (1.0 - 1.0 * fail / (n * (n - 1) / 2));
}