-
Notifications
You must be signed in to change notification settings - Fork 1
/
796 - Critical Links .cpp
93 lines (77 loc) · 2.07 KB
/
796 - Critical Links .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
88
89
90
91
92
93
/**
* author : Saurav Paul
* created : August 28, 2020 11:36 AM
* Problem Name : 796 - Critical Links
* Problem Limit : 3000 ms , 32 MB
* Problem Url : https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737
* @genarated by : ai-virtual-assistant
**/
#include<bits/stdc++.h>
using namespace std;
using ll = long long int ;
class TC{
int N ;
vector<vector<int>> graph ;
int timer = 0 ;
vector<int> tin , low ;
set<pair<int,int>> cut_bridges ;
vector<bool> vis ;
void dfs(int node, int par = -1){
vis[node] = true ;
low[node] = tin[node] = timer++ ;
for(int to : graph[node]){
if(to == par) continue ;
if(vis[to]){
low[node] = min(low[node] , tin[to]) ;
}
else{
dfs(to,node) ;
low[node] = min(low[node],low[to]) ;
if(low[to] > tin[node]){
int _u = node, _v = to ;
if(_u > _v) swap(_u,_v) ;
cut_bridges.insert({_u,_v}) ;
}
}
}
}
void find_cut_bridge(){
vis.assign(N+1,false) ;
tin.assign(N+1,-1) ;
low.assign(N+1,-1) ;
for(int i = 0 ; i < N ; i++){
if(!vis[i])
dfs(i) ;
}
int found = cut_bridges.size() ;
printf("%d critical links\n",found) ;
for(auto bridge : cut_bridges){
printf("%d - %d\n",bridge.first,bridge.second) ;
}
puts("") ;
}
public :
void solve(){
for(int i = 0 ; i < N ; i++){
int u , n , v;
scanf("%d (%d)",&u,&n) ;
while(n--){
scanf("%d",&v) ;
graph[u].emplace_back(v) ;
}
}
find_cut_bridge() ;
}
TC(int _n){
N = _n ;
graph = vector<vector<int>>(N+1) ;
}
};
int main(){
int n ;
while(scanf("%d",&n) != EOF){
TC tc(n);
tc.solve() ;
}
return 0 ;
}