-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathEC_P.cpp
65 lines (65 loc) · 1.71 KB
/
EC_P.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
// Ivan Carvalho
// Solution to https://www.spoj.com/problems/EC_P/
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int MAXN = 1010;
typedef pair<int, int> ii;
vector<int> grafo[MAXN];
vector<ii> pontes;
map<int, int> mapa[MAXN];
int num[MAXN], low[MAXN], n, m, TC, counter;
void dfs(int v, int p) {
num[v] = low[v] = ++counter;
for (int u : grafo[v]) {
if (u == p) continue;
if (num[u] == 0) {
dfs(u, v);
if (low[u] > num[v] && mapa[u][v] == 1) {
pontes.push_back(ii(min(v, u), max(v, u)));
}
low[v] = min(low[v], low[u]);
} else {
low[v] = min(low[v], num[u]);
}
}
}
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
cin >> TC;
for (int tc = 1; tc <= TC; tc++) {
cin >> n >> m;
counter = 0;
for (int i = 1; i <= n; i++) {
grafo[i].clear();
num[i] = low[i] = 0;
mapa[i].clear();
}
pontes.clear();
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
grafo[u].push_back(v);
grafo[v].push_back(u);
mapa[u][v]++;
mapa[v][u]++;
}
for (int i = 1; i <= n; i++) {
if (num[i] == 0) dfs(i, -1);
}
// cout << "Foi" << endl;
cout << "Caso #" << tc << endl;
sort(pontes.begin(), pontes.end());
if (pontes.empty()) {
cout << "Sin bloqueos" << endl;
} else {
cout << pontes.size() << endl;
}
for (ii par : pontes) {
cout << par.first << " " << par.second << endl;
}
}
return 0;
}