Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using pi32 = pair<int, int>;
- const int N = 1e5 + 5;
- int n, nE;
- vector<int> graph[N];
- int Num[N], Low[N], timer;
- stack<pi32> st;
- vector<int> best;
- void Tajan(int u, int p) {
- Num[u] = Low[u] = ++timer;
- for (int v : graph[u]) {
- if (v == p) continue;
- if (Num[v]) {
- Low[u] = min(Low[u], Num[v]);
- }
- else {
- st.emplace(u, v);
- Tajan(v, u);
- Low[u] = min (Low[u], Low[v]);
- if (Low[v] >= Num[u]) {
- pi32 t;
- pi32 last = {u, v};
- vector<int> path;
- do {
- t = st.top(); st.pop();
- path.push_back(t.second);
- } while (t != last);
- path.push_back(u);
- if (path.size() > best.size())
- swap(best, path);
- }
- }
- }
- }
- int main() {
- #ifdef LOCAL
- freopen("in.txt", "r", stdin);
- #else
- freopen("CJVUNGDATMOI.inp", "r", stdin);
- freopen("CJVUNGDATMOI.out", "w", stdout);
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cin >> n >> nE;
- for (int i = 1; i <= nE; ++i) {
- int u, v; cin >> u >> v;
- graph[u].push_back(v);
- graph[v].push_back(u);
- }
- for (int i = 1; i <= n; ++i)
- if (!Num[i]) Tajan(i, i);
- cout << best.size() << '\n';
- reverse(best.begin(), best.end());
- for (int u : best)
- cout << u << ' ';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment