Cod sursa(job #2374641)

Utilizator victorv88Veltan Victor victorv88 Data 7 martie 2019 19:46:02
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>
using namespace std;

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

int n, m, st, dr, ix[100005], llk[100005], viz[100005], indice=1, k;

vector<int>graph[100005];
vector<int>rez[100005];
stack<int>s;

void ctc(int ind)
{
    viz[ind]=1;
    ix[ind]=llk[ind]=indice++;
    s.push(ind);
    for (auto &v:graph[ind])
    {
        if (!ix[v])
        {
            ctc(v);
            llk[ind]=min(llk[ind],llk[v]);
        }
        else if (viz[v])
        {
            llk[ind]=min(llk[ind],llk[v]);
        }
    }
    if (ix[ind]==llk[ind])
    {
        while (!s.empty())
        {
            int nod=s.top();
            viz[nod]=0;
            s.pop();
            if (llk[nod]==ix[nod])
            {
                rez[k].push_back(nod);
                k++;
                break;
            }
            rez[k].push_back(nod);
        }
    }
}

int main()
{
    f >> n >> m;
    for (int i=0; i<m; ++i)
    {
        f >> st >> dr;
        graph[st].push_back(dr);
    }
    for (int i=1; i<=n; ++i)
    {
        if (!ix[i])
        {
            ctc(i);
        }
    }
    g << k << '\n';
    for (int i=0; i<k; ++i)
    {
        for (auto &v:rez[i])
        {
            g << v << ' ';
        }
        g << '\n';
    }
    return 0;
}