-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathD_Critical_Edges.cpp
More file actions
86 lines (77 loc) · 2.03 KB
/
D_Critical_Edges.cpp
File metadata and controls
86 lines (77 loc) · 2.03 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>
#define int long long
using namespace std;
int n, m;
vector<vector<int>> adj;
vector<int> levels, topmostlevelreachable;
vector<bool> visited;
vector<vector<int>> ans;
int caseNo = 0;
void dfs(int node, int parent, int level) {
visited[node] = true;
levels[node] = level;
topmostlevelreachable[node] = level;
for (int nb : adj[node]) {
if (nb == parent) continue;
if (!visited[nb]) {
dfs(nb, node, level + 1);
topmostlevelreachable[node] = min(topmostlevelreachable[node], topmostlevelreachable[nb]);
if (topmostlevelreachable[nb] > level) {
// record bridge; we'll normalize later
ans.push_back({ node, nb });
}
} else {
topmostlevelreachable[node] = min(topmostlevelreachable[node], levels[nb]);
}
}
}
void solve() {
cin >> n >> m;
adj.assign(n+1, {});
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
levels.assign(n+1, 0);
topmostlevelreachable.assign(n+1, numeric_limits<int>::max());
visited.assign(n+1, false);
ans.clear();
// graph is guaranteed connected
dfs(1, -1, 0);
// normalize so that u < v
for (auto &e : ans) {
if (e[0] > e[1])
swap(e[0], e[1]);
}
// sort by u, then v
sort(ans.begin(), ans.end(),
[](const vector<int>& A, const vector<int>& B) {
if (A[0] != B[0]) return A[0] < B[0];
return A[1] < B[1];
});
cout << "Caso #" << caseNo << "\n";
if (ans.empty()) {
cout << "Sin bloqueos\n";
} else {
cout << ans.size() << "\n";
for (auto &e : ans) {
cout << e[0] << " " << e[1] << "\n";
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int NC;
cin >> NC;
while (NC--) {
caseNo++;
solve();
}
return 0;
}