Cod sursa(job #2115480)

Utilizator IustinSSurubaru Iustin IustinS Data 26 ianuarie 2018 20:00:36
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include <fstream>
#include <vector>
#define nmax 100001
#define mmax 200001
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");

int n,m,k=1;
vector <int> lista[nmax];
vector <int> listaT[nmax];
int postord[nmax];
int ct;
bool fol[nmax];
vector <int> ctc[nmax];

void citire();
void DFS(int vf);
void DFSt(int vf);
void afisare();

int main()
{int i;
    citire();
    for (i=1; i<=n; i++)
        if (!fol[i])
            DFS(i);
    for (i=n; i>=1; i--)
        if (fol[postord[i]])
            {ct++;
             DFSt(postord[i]);
            }
    afisare();
    return 0;
}
void citire()
{
    int i,x,y;
    fin>>n>>m;
    for (i=1;i<=m; i++)
        {
            fin>>x>>y;
            lista[x].push_back(y);
            listaT[y].push_back(x);
        }
}
void DFS(int vf)
{
    int i;
    fol[vf]=1;
    for (i=0; i<lista[vf].size(); i++)
        if (!fol[lista[vf][i]])
            DFS(lista[vf][i]);
    postord[k++]=vf;
}
void DFSt(int vf)
{
    int i;
    fol[vf]=0;
    ctc[ct].push_back(vf);
    for (i=0; i<listaT[vf].size(); i++)
        if (fol[listaT[vf][i]])
            DFSt(listaT[vf][i]);
}
void afisare()
{
    fout<<ct<<'\n';
    for (int i=1; i<=ct; i++)
        {for (int j=0; j<ctc[i].size(); j++)
            fout<<ctc[i][j]<<' ';
        fout<<'\n';
        }

}