Advertisement
Sitleman

Bridges

Oct 17th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <iomanip>
  5. #include <vector>
  6. #include <tuple>
  7. #include <map>
  8. #include <set>
  9. #include <queue>
  10. #include <fstream>
  11. using namespace std;
  12. vector<vector<int>> g;
  13. vector<int> tin, up;
  14. vector<char> used;
  15. map<pair<int, int>, int> section;
  16. set<pair<int, int>> bridge;
  17. int timer = 0;
  18.  
  19. void dfs(int v, int p){
  20. timer++;
  21. used[v] = true;
  22. tin[v] = up[v] = timer;
  23. for (auto to : g[v]){
  24. if (to == p) continue;
  25. if (used[to]){
  26. up[v] = min(up[v], tin[to]);
  27. } else {
  28. dfs(to, v);
  29. up[v] = min(up[v], up[to]);
  30. if (up[to] > tin[v]){
  31. bridge.insert({to, v});
  32. bridge.insert({v, to});
  33. }
  34. }
  35. }
  36. }
  37.  
  38. int main() {
  39. ios::sync_with_stdio(0);
  40. cin.tie(0);
  41. cout.tie(0);
  42. int n, m;
  43. cin >> n >> m;
  44. g.resize(n + 1);
  45. tin.resize(n + 1);
  46. up.resize(n + 1);
  47. used.resize(n + 1, false);
  48. map<pair<int, int>, bool> f;
  49. for (int i = 1; i <= m; i++){
  50. int a, b;
  51. cin >> a >> b;
  52. if (section.count({a, b}) || section.count({b, a})){
  53. f[{a, b}] = true;
  54. f[{b, a}] = true;
  55. } else if (a != b){
  56. section[{a, b}] = i;
  57. g[a].push_back(b);
  58. g[b].push_back(a);
  59. }
  60. }
  61. for (int i = 1; i <= n; i++){
  62. if (!used[i]) dfs(i, -1);
  63. }
  64. int summ = 0;
  65. vector<int> ans;
  66. for (auto i: section){
  67. if (bridge.count(i.first) && !f[i.first]){
  68. summ++;
  69. ans.push_back(i.second);
  70. }
  71. }
  72. cout << summ << "\n";
  73. for (auto i: ans){
  74. cout << i << " ";
  75. }
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement