DuongNhi99

CJVUNGDATMOI (lqdoj) - SongLienThong

Mar 30th, 2021
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using pi32 = pair<int, int>;
  5.  
  6. const int N = 1e5 + 5;
  7.  
  8. int n, nE;
  9. vector<int> graph[N];
  10.  
  11. int Num[N], Low[N], timer;
  12. stack<pi32> st;
  13. vector<int> best;
  14.  
  15. void Tajan(int u, int p) {
  16.     Num[u] = Low[u] = ++timer;
  17.  
  18.     for (int v : graph[u]) {
  19.         if (v == p) continue;
  20.  
  21.         if (Num[v]) {
  22.             Low[u] = min(Low[u], Num[v]);
  23.         }
  24.         else {
  25.             st.emplace(u, v);
  26.             Tajan(v, u);
  27.             Low[u] = min (Low[u], Low[v]);
  28.  
  29.             if (Low[v] >= Num[u]) {
  30.                 pi32 t;
  31.                 pi32 last = {u, v};
  32.                 vector<int> path;
  33.  
  34.                 do {
  35.                     t = st.top(); st.pop();
  36.                     path.push_back(t.second);
  37.                 } while (t != last);
  38.                 path.push_back(u);
  39.  
  40.                 if (path.size() > best.size())
  41.                     swap(best, path);
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47. int main() {
  48. #ifdef LOCAL
  49.     freopen("in.txt", "r", stdin);
  50. #else
  51.     freopen("CJVUNGDATMOI.inp", "r", stdin);
  52.     freopen("CJVUNGDATMOI.out", "w", stdout);
  53. #endif
  54.     ios_base::sync_with_stdio(false);
  55.     cin.tie(nullptr);
  56.  
  57.     cin >> n >> nE;
  58.     for (int i = 1; i <= nE; ++i) {
  59.         int u, v; cin >> u >> v;
  60.         graph[u].push_back(v);
  61.         graph[v].push_back(u);
  62.     }
  63.  
  64.     for (int i = 1; i <= n; ++i)
  65.         if (!Num[i]) Tajan(i, i);
  66.  
  67.     cout << best.size() << '\n';
  68.     reverse(best.begin(), best.end());
  69.     for (int u : best)
  70.         cout << u << ' ';
  71.  
  72.     return 0;
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment