Cod sursa(job #1252810)

Utilizator PintilieAndreiPintilieAndrei PintilieAndrei Data 31 octombrie 2014 12:23:03
Problema Componente tare conexe Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
vector<int> a[100003],tr[100003];
int n,m,k;
int v[100003],v2[100003];
int tf[100003];
int ctc[100003];
ifstream fin("ctc.in");
ofstream fout("ctc.out");
void Read()
{
    fin>>n>>m;
    int i,x,y;
    for(i=1; i<=m; i++)
    {
        fin>>x>>y;
        a[x].push_back(y);
        tr[y].push_back(x);
    }
}

void DFStf(int x)
{
    v2[x]=1;
    for(int i=0; i<a[x].size(); i++)
    if(v2[a[x][i]]==0)
    DFStf(a[x][i]);
    tf[++k]=x;
}

void DFS(int x)
{
    v[x]=1;
    //cout<<x<<" ";
    ctc[x]=k;
    for(int i=0; i<tr[x].size(); i++)
    if(v[tr[x][i]]==0)
    {
        DFS(tr[x][i]);
    }

}

void graf_trans()
{
    int len,i,j,x;
    for(i=1; i<=n; i++)
    {
        len=a[i].size();
        for(j=0; j<len; j++)
        {
            x=a[i][j];
            tr[x].push_back(i);
        }
    }
}

void Afisare()
{
    int i;
    fout<<k<<"\n";
    for(i=1; i<=k; i++)
    {
        for(int j=1; j<=n; j++)
            if(ctc[j]==i)
            fout<<j<<" ";
        fout<<"\n";

    }
}

int main()
{
    Read();
    //graf_trans();
    k=0;
    for(int i=1; i<=n; i++)
    {
        if(v2[i]==0)
        DFStf(i);
    }
    k=0;
    for(int i=n; i>=1; i--)
    {

        //cout<<v[tf[i]]<<" ";
        if(v[tf[i]]==0)
        {
            k++;
           DFS(tf[i]);
           //cout<<"DFSADsa";
        }

    }


    Afisare();

    return 0;
}