Cod sursa(job #1676751)

Utilizator crysstyanIacob Paul Cristian crysstyan Data 6 aprilie 2016 09:45:58
Problema Componente biconexe Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.44 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#define NMAX 100005

using namespace std;

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

int i, n, m, cnt = 0, low[NMAX], lvl[NMAX], x, y;
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({it, node});
            DFS(it, node);
            low[node] = min(low[node], low[it]);

            if (low[it] >= lvl[node])
            {
                cnt ++;

                while (st.top().first != it && st.top().second != node)
                {
                    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;
}