Advertisement
ThegeekKnight16

Minas

May 14th, 2025
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #pragma GCC optimize("Ofast")
  4.  
  5. void dfs(int v, vector<int> &marc, const vector<vector<pair<int, int>>> &grafo, int &qnt, int x)
  6. {
  7.     marc[v] = 1; --qnt;
  8.     for (auto [viz, e] : grafo[v]) if (!marc[viz] && e != x) dfs(viz, marc, grafo, qnt, x);
  9. }
  10.  
  11. void getMatters(int v, vector<int> &marc, const vector<vector<pair<int, int>>> &grafo, vector<int> &matters)
  12. {
  13.     marc[v] = 1;
  14.     for (auto [viz, e] : grafo[v]) if (!marc[viz]) {matters[e] = 1; getMatters(viz, marc, grafo, matters);}
  15. }
  16.  
  17. int main()
  18. {
  19.     ios_base::sync_with_stdio(false); cin.tie(NULL);
  20.     int N, M; cin >> N >> M;
  21.     array<vector<vector<pair<int, int>>>, 2> grafo; grafo[0].resize(N+1); grafo[1].resize(N+1);
  22.     vector<pair<int, int>> edges(M);
  23.     for (int i = 0; i < M; i++) {auto &[x, y] = edges[i]; cin >> x >> y; grafo[0][x].emplace_back(y, i); grafo[1][y].emplace_back(x, i);}
  24.     vector<int> matters(M), marc(N+1);
  25.     getMatters(1, marc, grafo[0], matters);
  26.     fill(marc.begin(), marc.end(), 0);
  27.     getMatters(1, marc, grafo[1], matters);
  28.  
  29.     vector<pair<int, int>> resp; resp.reserve(M);
  30.     for (int i = 0; i < M; i++)
  31.     {
  32.         if (!matters[i]) continue;
  33.         auto [x, y] = edges[i];
  34.         vector<int> marc0(N+1, 0), marc1(N+1, 0); int qnt0 = N, qnt1 = N;
  35.         dfs(1, marc0, grafo[0], qnt0, i); dfs(1, marc1, grafo[1], qnt1, i);
  36.         if (qnt0 || qnt1) resp.emplace_back(x, y);
  37.     }
  38.  
  39.     cout << resp.size() << '\n';
  40.     for (auto [x, y] : resp) cout << x << " " << y << '\n';
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement