Cod sursa(job #1252822)

Utilizator PintilieAndreiPintilieAndrei PintilieAndrei Data 31 octombrie 2014 12:42:03
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.6 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
vector<int> a[100003],tr[100003],ct[100003];
int n,m,k;
int v[100003],v2[100003];
int tf[100003];
int ctc[100003];
ifstream fin("ctc.in");
ofstream fout("ctc.out");
inline 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);
    }
}

inline 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;
}

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

}

inline 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);
        }
    }
}

inline void Afisare()
{
    int i;
    fout<<k<<"\n";
    for(i=1; i<=k; i++)
    if(ct[i].size()>0)
    {
        for(int j=0; j<ct[i].size(); j++)
            //if(ctc[j]==i)
            fout<<ct[i][j]<<" ";
        fout<<"\n";

    }
}

int main()
{
    Read();
    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(ctc[tf[i]]==0)
        {
            k++;
           DFS(tf[i]);
           //cout<<"DFSADsa";
        }

    }


    Afisare();

    return 0;
}