Cod sursa(job #1145206)

Utilizator dan.ghitaDan Ghita dan.ghita Data 17 martie 2014 23:07:14
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
int n, m, a, b, k;
bool checked[100005];
vector<int> lv[100005], lv_transpus[100005], stck, out[100005];

void df(int x){
    checked[x]=1;
    vector<int>::iterator it;
    for(it=lv[x].begin(); it != lv[x].end(); ++it)
        if(!checked[*it])
            df(*it);
    stck.push_back(x);
}

void df_transpus(int x){
    checked[x]=1;
    out[k].push_back(x);
    vector<int>::iterator it;
    for(it=lv_transpus[x].begin(); it != lv_transpus[x].end(); ++it)
        if(!checked[*it])
            df_transpus(*it);
}

int main()
{
    f>>n>>m;
    for(int i=0; i<m ; ++i)
    {
        f>>a>>b;
        lv[a].push_back(b);
        lv_transpus[b].push_back(a);
    }
    for(int i=1; i<=n; ++i)
        if(!checked[i])
            df(i);
    for(int i=1; i<=n; ++i)
        checked[i]=0;
    while(!stck.empty()){
        if(!checked[stck.back()])
            ++k, df_transpus(stck.back());
            stck.pop_back();
    }
    g<<k<<'\n';
    while(k){
        for(int i=0; i<out[k].size(); ++i)
            g<<out[k][i]<<' ';
        g<<'\n';
        k--;
    }



    return 0;
}