-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimportant_bridges.cpp
84 lines (67 loc) · 2 KB
/
important_bridges.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
// STL includes
#include <iostream>
#include <vector>
// BGL includes
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/biconnected_components.hpp>
namespace boost {
struct edge_component_t {
enum { num = 555 };
typedef edge_property_tag kind;
} edge_component;
}
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, boost::property<boost::edge_component_t, std::size_t >> graph;
typedef boost::graph_traits<graph>::edge_descriptor edge_desc;
typedef boost::graph_traits<graph>::vertex_descriptor vertex_desc;
using namespace std;
struct edge {
int u;
int v;
bool operator<(edge &o) const {
return u < o.u || (u == o.u && v < o.v);
}
};
void solve() {
int n;
cin >> n;
int m;
cin >> m;
graph G(n);
int u, v;
for (int i = 0; i < m; ++i) {
cin >> u;
cin >> v;
boost::add_edge(u, v, G);
}
boost::property_map<graph, boost::edge_component_t>::type component = get(boost::edge_component, G);
int ncc = biconnected_components(G, component);
vector<vector<edge_desc>> edgesOfComponent(ncc, vector<edge_desc>());
boost::graph_traits<graph>::edge_iterator ei, ei_end;
for (boost::tie(ei, ei_end) = edges(G); ei != ei_end; ++ei) {
edgesOfComponent[component[*ei]].push_back(*ei);
}
vector<edge> result;
for (int i = 0; i < ncc; ++i) {
if (edgesOfComponent[i].size() == 1) {
u = source(edgesOfComponent[i][0], G);
v = target(edgesOfComponent[i][0], G);
if (v < u) {
u = target(edgesOfComponent[i][0], G);
v = source(edgesOfComponent[i][0], G);
}
result.push_back({u, v});
}
}
sort(result.begin(), result.end());
cout << result.size() << endl;
for (auto e : result) {
cout << e.u << " " << e.v << endl;
}
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
solve();
}
}