Cod sursa(job #2299005)

Utilizator victorv88Veltan Victor victorv88 Data 8 decembrie 2018 18:47:26
Problema Componente tare conexe Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define MOD 9973
std :: ifstream f("ctc.in");
std :: ofstream g("ctc.out");

using namespace std;

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

int n, m, from, to, llk[100005], ix[100005], viz[100005],indice_actual=1,nr;

void dfs(int ind)
{
    viz[ind]=1;
    ix[ind]=llk[ind]=indice_actual++;
    s.push(ind);
    for (auto &v:graph[ind])
    {
        if (ix[v]==0)
        {
            dfs(v);
            llk[ind]=min(llk[ind],llk[v]);
        }
        else if (viz[ind]==1)
        {
            llk[ind]=min(llk[ind],llk[v]);
        }
    }
    if (llk[ind]==ix[ind])
    {
        while (!s.empty())
        {
            int element=s.top();
            s.pop();
            viz[element]=0;
            sol[nr].push_back(element);
            if (ix[element]==llk[element])
            {
                nr++;
                break;
            }
        }
    }
}

int main() {
    f >> n >> m;
    for (int i=1; i<=m; i++)
    {
        f >> from >> to;
        graph[from].push_back(to);
    }
    for (int i=1; i<=n; i++)
    {
        if (ix[i]==0)
        {
            dfs(i);
        }
    }
    g << nr << '\n';
    for (int i=0; i<nr; i++)
    {
        for (auto &v:sol[i])
            g << v <<' ';
        g << '\n';
    }
    return 0;
}