-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimportant-bridge.cpp
75 lines (64 loc) · 1.74 KB
/
important-bridge.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
#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>
#include <list>
#include <boost/config.hpp>
#include <boost/graph/biconnected_components.hpp>
#include <boost/graph/adjacency_list.hpp>
using namespace std;
using namespace boost;
namespace boost {
struct edge_component_t {
enum { num = 555 };
typedef edge_property_tag kind;
}
edge_component;
}
typedef adjacency_list<vecS, vecS, undirectedS, no_property, property<edge_component_t, int> > Graph;
typedef graph_traits<Graph>::vertex_descriptor Vertex;
typedef graph_traits<Graph>::edge_descriptor Edge;
typedef graph_traits<Graph>::edge_iterator EdgeIt;
typedef property_map<Graph, edge_component_t>::type EdgeComponent;
void bridge() {
int nV, nE;
cin >> nV >> nE;
Graph G(nV);
EdgeComponent comp = get(edge_component, G);
for (int i = 0; i < nE; i++) {
int u, v;
cin >> u >> v;
add_edge(u, v, G);
}
int nComp = biconnected_components(G, comp);
vector< vector< pair<int, int> > > compEdge(nComp);
EdgeIt ebeg, eend;
for (tie(ebeg, eend) = edges(G); ebeg != eend; ++ebeg) {
Vertex u = source(*ebeg, G);
Vertex v = target(*ebeg, G);
compEdge[comp[*ebeg]].push_back(make_pair(u, v));
}
vector< pair<int, int> > bridge;
for (int i = 0; i < compEdge.size(); ++i) {
if (compEdge[i].size() == 1) {
if (compEdge[i][0].first < compEdge[i][0].second) {
bridge.push_back(compEdge[i][0]);
} else {
bridge.push_back(make_pair(compEdge[i][0].second, compEdge[i][0].first));
}
}
}
sort(bridge.begin(), bridge.end());
cout << bridge.size() << endl;
for (int i = 0; i < bridge.size(); i++) {
cout << bridge[i].first << " " << bridge[i].second << endl;
}
}
int main(void) {
int T;
cin >> T;
for (int t = 0; t < T; t++) {
bridge();
}
return 0;
}