-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpoj_Critical_edges.cpp
141 lines (128 loc) · 2.09 KB
/
Spoj_Critical_edges.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
maneesh(maik)
*/
// Bridges in undirected graph
// Pre and Post time of DFS
#include<iostream>
#include<algorithm>
#include<string>
#include<bitset>
#include<deque>
#include<iterator>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define M 1000000007
#define fastio ios_base::sync_with_stdio(false);
struct data
{
ll u;
ll v;
};
bool comp(data a, data b)
{
if(a.u==b.u)
return a.v<b.v;
else
return a.u<b.u;
}
ll tim;
void DFS(ll s, ll p, ll vis[], ll Pre[], ll Post[], ll A[], vector<ll>G[], vector<data>&edge)
{
vis[s]=1;
for(ll i=0; i<G[s].size(); i++)
{
ll it=G[s][i];
if(vis[it]==0)
{
Pre[it]=tim; A[it]=tim;
tim++;
DFS(it, s, vis, Pre, Post, A, G, edge);
}
}
Post[s]=tim; tim++;
/*cout<<s<<" "<<p<<endl;
cout<<Pre[s]<<" "<<A[s]<<endl;
cout<<Pre[p]<<" "<<A[p]<<endl;
cout<<endl;*/
ll t;
for(ll i=0; i<G[s].size(); i++)
{
ll it=G[s][i];
if(it!=p)
{
t=min(Pre[it], A[it]);
A[s]=min(A[s], t);
}
}
if(A[s]>Pre[p])
{
data z;
z.u=min(s,p);
z.v=max(s,p);
edge.pb(z);
//cout<<edge.size()<<endl;
}
}
int main()
{
fastio;
ll t,tc;
cin>>t;
for(tc=1; tc<=t; tc++)
{
ll n,m;
cin>>n>>m;
vector<data>edge;
data z;
ll i,x,y;
vector<ll>G[n+2];
ll Pre[n+3],Post[n+3],A[n+3];
for(i=0; i<m; i++)
{
cin>>x>>y;
G[x].pb(y);
G[y].pb(x);
}
ll vis[n+3];
for(i=1; i<=n; i++)
vis[i]=0;
ll cnt=0;
tim=1;
A[1]=1; Pre[1]=1; vis[1]=1; tim++;
DFS(1, 1, vis, Pre, Post, A, G, edge);
/*for(i=1; i<=n; i++)
cout<<P[i]<<" ";*/
/*for(i=1; i<=n; i++)
{
cout<<i<<"--> ";
for(ll j=0; j<G[i].size(); j++)
cout<<G[i][j]<<" ";
cout<<endl;
}*/
cout<<"Caso #"<<tc<<endl;
if(edge.size()==0)
{
cout<<"Sin bloqueos"<<endl;
}
else
{
cout<<edge.size()<<endl;
sort(edge.begin(), edge.end(), comp);
for(i=0; i<edge.size(); i++)
{
cout<<edge[i].u<<" "<<edge[i].v<<endl;
}
}
}
return 0;
}