Cod sursa(job #1147784)

Utilizator TwoOfDiamondsDaniel Alexandru Radu TwoOfDiamonds Data 20 martie 2014 09:48:08
Problema Componente biconexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
#include <fstream>
#include <stack>
#include <vector>
#include <list>

using namespace std;

const int MAXN = 100001;
list<int> vecini[MAXN];
stack<int> muchii[2];
vector<int> componente[MAXN];
bool viz[MAXN];
int depth[MAXN], up[MAXN];
int nrComp;

void dfs(int vertex)
{
    viz[vertex] = true;
    up[vertex] = depth[vertex];

    for(auto& it : vecini[vertex])
    {
        if(!viz[it])
        {
            depth[it] = depth[vertex] + 1;

            muchii[0].push(vertex);
            muchii[1].push(it);

            dfs(it);

            if(up[it] >= depth[vertex])
            {
                nrComp++;

                while(muchii[0].top() != vertex || muchii[1].top() != it)
                    componente[nrComp].push_back(muchii[1].top()), muchii[0].pop(), muchii[1].pop();

                componente[nrComp].push_back(muchii[0].top());
                componente[nrComp].push_back(muchii[1].top());

                muchii[0].pop();
                muchii[1].pop();
            }

            up[vertex] = up[it] < up[vertex] ? up[it] : up[vertex];
        }
        else
        {
            up[vertex] = depth[it] < up[vertex] ? depth[it] : up[vertex];
        }
    }
}

int main()
{
    ifstream IN("biconex.in");
    int n, m;
    IN >> n >> m;

    for (int i = 0 ; i < m; i++)
    {
        int x, y; IN >> x >> y;
        vecini[x].push_back(y);
        vecini[y].push_back(x);
    }

    IN.close();

    ofstream OUT("biconex.out");

    OUT << nrComp << "\n";

    for (int i = 0 ; i < nrComp; i++)
    {
        for(auto& it : componente[nrComp])
            OUT << it << " ";
        OUT << endl;
    }

    return 0;
}