Guest User

Untitled

a guest
Jul 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <vector>
  4. #include <boost/graph/adjacency_list.hpp>
  5. #include <boost/graph/biconnected_components.hpp>
  6.  
  7. using namespace std;
  8. using namespace boost;
  9.  
  10. struct edge_component_t {
  11. typedef edge_property_tag kind;
  12. };
  13.  
  14. typedef adjacency_list<listS, vecS, undirectedS, no_property, property<edge_component_t, int> > Graph;
  15. typedef graph_traits<Graph>::vertex_descriptor Vertex;
  16. typedef graph_traits<Graph>::edge_descriptor Edge;
  17. typedef graph_traits<Graph>::edge_iterator EdgeIt;
  18. typedef property_map<Graph, edge_component_t>::type ComponentMap;
  19.  
  20. struct Functor
  21. {
  22. bool operator()(pair<int,int>& p1, pair<int,int>& p2)
  23. {
  24. return p1.first == p2.first ? p1.second < p2.second : p1.first < p2.first;
  25. }
  26. };
  27.  
  28. int main(int argc, char *argv[])
  29. {
  30. int cases, n, m, s, e;
  31. cin >> cases;
  32.  
  33. for (int i = 0; i < cases; ++i)
  34. {
  35. cin >> n >> m;
  36.  
  37. Graph g(n);
  38.  
  39. for (int j = 0; j < m; ++j)
  40. {
  41. cin >> s >> e;
  42.  
  43. bool success;
  44. Edge edge;
  45. tie(edge, success) = add_edge(s-1, e-1, g);
  46. }
  47.  
  48. if (n <= 1)
  49. {
  50. cout << '0' << endl;
  51. continue;
  52. }
  53.  
  54. ComponentMap components = get(edge_component_t(), g);
  55. int n_components = biconnected_components(g, components);
  56.  
  57. vector<int> components_size(n_components);
  58.  
  59. EdgeIt it, it_end;
  60. for (tie(it, it_end) = edges(g); it != it_end; ++it)
  61. components_size[components[*it]]++;
  62.  
  63. list<pair<int,int> > critical;
  64.  
  65. for (tie(it, it_end) = edges(g); it != it_end; ++it)
  66. if (components_size[components[*it]] == 1)
  67. if (source(*it, g) < target(*it, g))
  68. critical.push_back(make_pair(source(*it,g)+1, target(*it,g)+1));
  69. else
  70. critical.push_back(make_pair(target(*it,g)+1, source(*it,g)+1));
  71.  
  72. cout << critical.size() << endl;
  73.  
  74. Functor f;
  75. critical.sort(f);
  76.  
  77. for (list<pair<int,int> >::iterator k = critical.begin(); k != critical.end(); ++k)
  78. cout << k->first << ' ' << k->second << endl;
  79. }
  80.  
  81. return 0;
  82. }
Add Comment
Please, Sign In to add comment