Cod sursa(job #2574578)

Utilizator andreichiricaAndrei Chirica andreichirica Data 6 martie 2020 00:02:13
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include <bits/stdc++.h>
#define nmax 100005

using namespace std;

ifstream fin("ctc.in");
ofstream fout("ctc.out");

int noduri, muchii, componente_tari, a, b, ind = 1, ids[nmax];
bool instack[nmax];
int lowkey[nmax];
vector<int> mat[nmax];
vector<int> componente[nmax];
stack<int> s;

void dfs(int node)
{
    s.push(node);
    instack[node] = 1;
    ids[node] = ind;
    lowkey[node] = ind;
    ++ind;
    for(int i = 0; i < mat[node].size(); ++i)
    {
        int next_node = mat[node][i];
        if(!ids[next_node])
        {
            dfs(next_node);
        }
        if(instack[next_node])
        {
            lowkey[node] = min(lowkey[node], lowkey[next_node]);
        }
    }
    if(ids[node] == lowkey[node])
    {
        while(!s.empty())
        {
            int next = s.top();
            componente[componente_tari].push_back(next);
            s.pop();
            instack[next] = 0;
            lowkey[next] = ids[node];
            if(node == next) break;
        }
        componente_tari++;
    }
}

int main()
{
    fin >> noduri >> muchii;
    for(int i = 1; i <= muchii; ++i)
    {
        fin >> a >> b;
        mat[a].push_back(b);
    }
    for(int node = 1; node <= noduri; ++node)
    {
        if(!ids[node])
        {
            dfs(node);
        }
    }
    fout << componente_tari << "\n";
    for(int i = 0; i < componente_tari; ++i)
    {
        for(int j = 0; j < componente[i].size(); ++j)
        {
            fout << componente[i][j] << " ";
        }
        fout << "\n";
    }
    return 0;
}