Cod sursa(job #1038430)

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

vector<int> vf[NMAX];
vector<int> vf_transpus[NMAX];
int postordine[NMAX], vizitat[NMAX], cc[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, int c){
    vizitat[x] = 0;
    cc[x] = c;
    for(int i = 0; i < vf[x].size(); i++){
        if(vizitat[vf[x][i]]){
            DFS(vf[x][i], c);
        }
    }
}

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);
            conex++;
        }
    }
    printf("%d\n", conex); conex--;
    for(i = conex; i >= 0; i--){
        for(j = 1; j <= N; j++){
            if(cc[j] == i){
                printf("%d ", j);
            }
        }
        printf("\n");
    }
    return 0;
}