Cod sursa(job #1649985)

Utilizator andreismara97Smarandoiu Andrei andreismara97 Data 11 martie 2016 16:04:11
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include<fstream>
#include<vector>
using namespace std;

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

const int Nmax = 100001;
vector <int> lista[Nmax];
vector <int> transp[Nmax];
vector <int> sol[Nmax];
bool viz[Nmax]={false};

int post[Nmax];
int n,m,ct=0;

void citire()
{
    in>>n>>m;
    for(int i=1; i<=m; i++)
    {
        int x,y;
        in>>x>>y;
        lista[x].push_back(y);
        transp[y].push_back(x);
    }
}

void DFS(int nod)
{
    viz[nod]=true;
    for(int i=0; i<lista[nod].size(); i++)
        if(viz[lista[nod][i]] == false)
            DFS(lista[nod][i]);
    post[++ct]=nod;
}

void tDFS(int nod)
{
    viz[nod]=false;
    sol[ct].push_back(nod);
    for(int i=0; i<transp[nod].size(); i++)
        if(viz[transp[nod][i]] == true)
            tDFS(transp[nod][i]);
}

int main ()
{
    citire();
    for(int i=1; i<=n; i++)
        if(viz[i] == false)
            DFS(i);
    ct=0;
    for(int i=n; i>=1; i--)
        if(viz[post[i]] == true)
        {
            ct++;
            tDFS(post[i]);
        }
    out<<ct<<'\n';
    for(int i=1; i<=ct; i++)
    {
        for(int j=0; j<sol[i].size(); j++)
                out<<sol[i][j]<<' ';
        out<<'\n';
    }
    return 0;
}