Cod sursa(job #1220139)

Utilizator Cristian1997Vintur Cristian Cristian1997 Data 16 august 2014 16:52:01
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
using namespace std;
#include <fstream>
#include <vector>
ofstream fout("ctc.out");
#define Nmax 100001

int n, nr = 0;
bool uz[Nmax];
int ord[Nmax];
vector <int> G[Nmax], GT[Nmax], comp[Nmax];

void citire() ;
void DFS(int) ;
void detComp(int) ;


int main()
{
    int i;
    citire();
    for(i = 1; i <= n; ++i)
        if(!uz[i]) DFS(i);
    for(i = 1; i <= n; ++i) uz[i] = 0;
    
    nr = 0;
    for(i = n; i >= 1; --i)
        if(!uz[ord[i]])
        {
            ++nr;
            detComp(ord[i]);
        }
    fout << nr << '\n';
    for(i = 1; i <= nr; ++i)
    {
        for(vector<int>::iterator it = comp[i].begin(); it != comp[i].end(); ++it)
            fout << *it << ' ';
        fout << '\n';
    }
    return 0;
}

void citire()
{
    ifstream fin("ctc.in");
    int m, x, y;
    fin >> n >> m;
    for(; m; --m)
    {
        fin >> x >> y;
        G[x].push_back(y);
        GT[y].push_back(x);
    }
}


void DFS(int vf)
{
    vector<int>::iterator it;
    uz[vf] = 1;
    for(it = G[vf].begin(); it != G[vf].end(); ++it)
        if(!uz[*it])
        {
            uz[*it] = 1;
            DFS(*it);
        }
    ord[++nr] = vf;
}



void detComp(int vf)
{
    comp[nr].push_back(vf);
    
    vector<int>::iterator it;
    uz[vf] = 1;
    for(it = GT[vf].begin(); it != GT[vf].end(); ++it)
        if(!uz[*it])
        {
            uz[*it] = 1;
            detComp(*it);
        }
}