-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.cpp
54 lines (43 loc) · 1.25 KB
/
algorithm.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
#include <limits>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/biconnected_components.hpp>
using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS>;
using EdgeDesc = boost::graph_traits<Graph>::edge_descriptor;
void testcase() {
int n, m; std::cin >> n >> m;
Graph g(n);
std::map<EdgeDesc, int> map;
for (int i = 0; i < m; i++) {
int a, b; std::cin >> a >> b;
boost::add_edge(a, b, g);
}
auto bc = boost::biconnected_components(g, boost::make_assoc_property_map(map));
std::vector<int> counts(bc, 0);
for (const auto i: map)
counts[i.second]++;
int k = 0;
std::set<std::pair<int, int>> critical;
for (const auto i: map)
if (counts[i.second] == 1) {
k++;
int src = boost::source(i.first, g), tgt = boost::target(i.first, g);
critical.insert(std::make_pair(std::min(src, tgt), std::max(src, tgt)));
}
std::cout << k << std::endl;
for (const auto i: critical)
std::cout << i.first << " " << i.second << "\n";
}
int main() {
std::ios_base::sync_with_stdio(false);
int t;
std::cin >> t;
for (int i = 0; i < t; ++i)
testcase();
}