Cod sursa(job #1603039)

Utilizator Burbon13Burbon13 Burbon13 Data 17 februarie 2016 09:40:18
Problema Componente tare conexe Scor 28
Compilator cpp Status done
Runda Arhiva educationala Marime 1.56 kb
#include <cstdio>
#include <vector>
#include <bitset>

using namespace std;

const int nmx = 100002;

int n,m,nrg;
vector <int> g[nmx],srt,rez;
bitset <nmx> viz;

void input() {
    scanf("%d %d", &n, &m);
    int nod1,nod2;
    for(int i = 1; i <= m; ++i) {
        scanf("%d %d", &nod1, &nod2);
        g[nod1].push_back(nod2);
    }
}

void sortaret(int nod) {
    for(vector<int>::iterator it = g[nod].begin(); it != g[nod].end(); ++it)
        if(not viz[*it]) {
            viz[*it] = true;
            sortaret(*it);
        }
    srt.push_back(nod);
}

void componente(int nod) {
    rez.push_back(nod);
    for(vector<int>::iterator it = g[nod].begin(); it != g[nod].end(); ++it)
        if(not viz[*it]) {
            viz[*it] = true;
            componente(*it);
        }
}

int main() {
    freopen("ctc.in", "r", stdin);
    freopen("ctc.out", "w", stdout);

    input();

    for(int i = 1; i <= n; ++i)
        if(not viz[i]) {
            viz[i] = true;
            sortaret(i);
        }

    viz.reset();

    for(vector<int>::iterator it = srt.begin(); it != srt.end(); ++it)
        if(not viz[*it]) {
            ++ nrg;
            int aux = rez.size();
            viz[*it] = true;
            componente(*it);
            rez.push_back(rez.size()-aux);
        }

    printf("%d\n", nrg);
    for(vector<int>::reverse_iterator it = rez.rbegin(); it != rez.rend(); ++it){
        int val =  *it;
        while(val--)
            printf("%d ", *(it++));
        printf("\n");
    }

    return 0;
}