Cod sursa(job #935639)

Utilizator UnforgivenMihai Catalin Botezatu Unforgiven Data 4 aprilie 2013 13:04:36
Problema Componente tare conexe Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.48 kb
#include <fstream>
#include <cstdlib>
#include <vector>

using namespace std;

const static int nmax = 100001;

vector <int> graf[nmax];
vector <int> transpus[nmax];
vector <int> ctc[nmax];
vector <int> aux;

int sel[nmax];
int n , m;
int nrctc = 0;

ifstream in("ctc.in");
ofstream out("ctc.out");

void DF_Graf(int nod)
{
    sel[nod] = 1;
    aux.push_back(nod);
    for (int i =0;i<graf[nod].size();i++)
    {
        int next = graf[nod][i];
        if (sel[next] == 0) DF_Graf(next);
    }
}

void DF_Transpus(int nod)
{
    sel[nod] = 2;
    ctc[nrctc].push_back(nod);
    for (int i =0;i<transpus[nod].size();i++)
    {
        int next = transpus[nod][i];
        if (sel[next] == 1) DF_Transpus(next);
    }
}

int main()
{

    in >> n >> m;
    for (int i =0;i<m;i++)
    {
        int x , y;
        in >> x >> y;
        graf[x].push_back(y);
        transpus[y].push_back(x);
    }

    fill(sel,sel+n+1,0);

    for (int i = 1;i<=n;i++)
    {
        if (sel[i] == 0)
        {
            aux.clear();
            DF_Graf(i);
            DF_Transpus(i);
            for (int j =0;j<aux.size();j++)
            {
                if (sel[aux[j]] == 1) sel[aux[j]] = 0;
            }
            nrctc++;
        }
    }

    out << nrctc;
    for (int i =0;i<nrctc;i++)
    {
        out << "\n";
        for (int j =0;j<ctc[i].size();j++)
        {
            out << ctc[i][j] << " ";
        }
    }


    return 0;
}