Cod sursa(job #1610072)

Utilizator ovidiuz98Zamfir Ovidiu ovidiuz98 Data 23 februarie 2016 11:27:22
Problema Componente tare conexe Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <algorithm>
#define DIM 100005

using namespace std;

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

int N,M,k;
int s[DIM];
bitset <DIM> viz;
vector <int> v[DIM],sol[DIM];

void DFS(int x){
    viz[x]=1;
    for(int i=0;i<v[x].size();i++)
        if(!viz[v[x][i]])
            DFS(v[x][i]);
    s[++k]=x;
}

int DFST(int x){
    viz[x]=0;
    sol[k].push_back(x);
    for(int i=0;i<v[x].size();i++)
        if(viz[v[x][i]])
            DFST(v[x][i]);
}

int main(){

    fin >> N >> M;

    while(M--){
        int x,y;
        fin >> x >> y;
        v[x].push_back(y);
    }

    for(int i=1;i<=N;i++)
        if(!viz[i])
            DFS(i);

    k=0;
    for(int i=1;i<=N;i++){
        if(viz[s[i]]){
            k++;
            DFST(s[i]);
        }
    }

    fout << k << "\n";

    for(int i=1;i<=k;i++){
        sort(sol[i].begin(),sol[i].end());
        for(std::vector <int>::iterator it=sol[i].begin();it!=sol[i].end();it++)
            fout << *it << " ";
        fout << "\n";
    }

}