Cod sursa(job #2842733)

Utilizator DavidGoldandavid teo DavidGoldan Data 1 februarie 2022 13:35:41
Problema Componente tare conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <fstream>
#include <vector>
#define NMAX 100002
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");

int n, nr, m, poz;
vector<int> G[NMAX];
vector<int> GT[NMAX];
bool viz[NMAX];
vector<int> ctc[NMAX];
int postordine[NMAX];

void citire();
void DFST(int x);
void afisare();
void DFS(int x);

int main()
{
    citire();
    for(int i=1; i<=n; i++)
        if(viz[i]==0)
           DFS(i);
    for(int i=n; i>0; i++)
       if(viz[postordine[i]]==1){
          nr++;
          DFST(postordine[i]);
    }
    afisare();
}

void citire(){
    int x, y;
    fin >> n >> m;
    for(int i=0; i<m; i++){
        fin >> x >> y;
        G[x].push_back(y);
        GT[y].push_back(x);
    }

}

void DFS(int x){
    int i;
    viz[x]=i;
    for(i=0; i<G[x].size(); i++)
        if(viz[G[x][i]]==0)
           DFS(G[x][i]);
    postordine[++poz]=x;
}

void DFST(int x){
    int i;
    viz[x]=0;
    ctc[nr].push_back(x);
    for(i=0; i<GT[x].size(); i++)
        if(viz[GT[x][i]]==0)
           DFS(GT[x][i]);
    postordine[++poz]=x;
}
void afisare(){
    fout << nr <<'\n';
    for(int i=1; i<=nr; i++){
        for(int j=0; j<G[i].size(); j++)
            fout << ctc[i][j]<<' ';
        fout << '\n';
    }
}