Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <vector>
- #include <boost/graph/adjacency_list.hpp>
- #include <boost/graph/biconnected_components.hpp>
- using namespace std;
- using namespace boost;
- struct edge_component_t {
- typedef edge_property_tag kind;
- };
- typedef adjacency_list<listS, 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 ComponentMap;
- struct Functor
- {
- bool operator()(pair<int,int>& p1, pair<int,int>& p2)
- {
- return p1.first == p2.first ? p1.second < p2.second : p1.first < p2.first;
- }
- };
- int main(int argc, char *argv[])
- {
- int cases, n, m, s, e;
- cin >> cases;
- for (int i = 0; i < cases; ++i)
- {
- cin >> n >> m;
- Graph g(n);
- for (int j = 0; j < m; ++j)
- {
- cin >> s >> e;
- bool success;
- Edge edge;
- tie(edge, success) = add_edge(s-1, e-1, g);
- }
- if (n <= 1)
- {
- cout << '0' << endl;
- continue;
- }
- ComponentMap components = get(edge_component_t(), g);
- int n_components = biconnected_components(g, components);
- vector<int> components_size(n_components);
- EdgeIt it, it_end;
- for (tie(it, it_end) = edges(g); it != it_end; ++it)
- components_size[components[*it]]++;
- list<pair<int,int> > critical;
- for (tie(it, it_end) = edges(g); it != it_end; ++it)
- if (components_size[components[*it]] == 1)
- if (source(*it, g) < target(*it, g))
- critical.push_back(make_pair(source(*it,g)+1, target(*it,g)+1));
- else
- critical.push_back(make_pair(target(*it,g)+1, source(*it,g)+1));
- cout << critical.size() << endl;
- Functor f;
- critical.sort(f);
- for (list<pair<int,int> >::iterator k = critical.begin(); k != critical.end(); ++k)
- cout << k->first << ' ' << k->second << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment