Cod sursa(job #1038439)

Utilizator caliuxSegarceanu Calin caliux Data 21 noiembrie 2013 15:30:11
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include <stdio.h>
#include <vector>
#define NMAX 100005
using namespace std;

vector<int> vf[NMAX];
vector<int> vf_transpus[NMAX];
vector<int> cc[NMAX];
int postordine[NMAX], vizitat[NMAX];
int N, M, contor, conex;

void DFST(int x){
    vizitat[x] = 1;
    for(int i = 0; i < vf_transpus[x].size(); i++){
        if(vizitat[vf_transpus[x][i]] == 0){
            DFST(vf_transpus[x][i]);
        }
    }
    postordine[++contor] = x;
}

void DFS(int x){
    vizitat[x] = 0;
    cc[conex].push_back(x);
    for(int i = 0; i < vf[x].size(); i++){
        if(vizitat[vf[x][i]]){
            DFS(vf[x][i]);
        }
    }
}

int main()
{
    freopen("ctc.in", "r", stdin);
    freopen("ctc.out", "w", stdout);
    int i, j, x, y;
    scanf("%d%d", &N, &M);
    for(i = 1; i <= M; i++){
        scanf("%d%d", &x, &y);
        vf[x].push_back(y);
        vf_transpus[y].push_back(x);
    }
    for(i = 1; i <= N; i++){
        if(vizitat[i] == 0){
            DFST(i);
        }
    }
    for(i = N; i >= 1; i--){
        if(vizitat[postordine[i]]){
            DFS(postordine[i]);
            conex++;
        }
    }
    printf("%d\n", conex); conex--;
    for(i = conex; i >= 0; i--){
       for(j = 0; j < cc[i].size(); j++){
            printf("%d ", cc[i][j]);
       }
       printf("\n");
    }
    return 0;
}