Cod sursa(job #1676718)

Utilizator crysstyanIacob Paul Cristian crysstyan Data 6 aprilie 2016 09:21:44
Problema Componente biconexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
#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;
}