Cod sursa(job #2697124)

Utilizator QubeeStefan Ste Qubee Data 17 ianuarie 2021 18:28:10
Problema Subsir Scor 0
Compilator c-64 Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <bits/stdc++.h>

#define NMAX 100005

using namespace std;

ifstream f("biconex.in");
ofstream g("biconex.out");

int i, n, m, x, y, low[NMAX], lvl[NMAX], cnt = 0;
bool used[NMAX];
vector < int > v[NMAX], ans[NMAX];
stack < pair < int, int > > st;

void DFS(int node, int father){

    used[node] = 1;
    lvl[node] = lvl[father] + 1;
    low[node] = lvl[node];

    for (auto & it : v[node]){
        if (used[it])
            low[node] = min(low[node], lvl[it]);

        else{

            st.push({node, it});
            DFS(it, node);
            low[node] = min(low[node], low[it]);

            if (low[it] >= lvl[node]){
                cnt ++;
                while (!st.empty() && st.top().first != node && st.top().second != it){

                    ans[cnt].push_back({st.top().second});
                    st.pop();
                }

                ans[cnt].push_back(node);
                ans[cnt].push_back(it);
                st.pop();
            }
        }
    }
}

int main(){

    f >> n >> m;
    for (i = 1; i <= m; ++ i){
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    DFS(1, 0);

    g << cnt << '\n';

    for (i = 1; i <= cnt; ++ i){
        for (auto & it : ans[i])
            g << it << " ";
        g << '\n';
    }
    return 0;
}