Cod sursa(job #1068782)

Utilizator raulmuresanRaul Muresan raulmuresan Data 28 decembrie 2013 18:58:18
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.57 kb
#include <fstream>
#include <algorithm>
#include<vector>

using namespace std;

int b,componente,i,aux,n,k,j,p,s,unu,t,m,doi,x,q,st,dr,maxi,maxi2,viz[100010],y,conex,postordine[100100];
vector <int> v[100010],w[100010],sol[100010];


 ifstream fin("ctc.in");
 ofstream fout("ctc.out");
void clean()
{
    int j;
    for(j=1;j<=n;j++) viz[j]=0;
}

void dfs(int x) {
    int i, y;
    viz[x] = 1;
    //printf("%d ",x);
    for (i = 0; i < v[x].size(); i++) {
        y = v[x][i];
        if (viz[y]==0)
            dfs(y);
    }
    k++;
    postordine[k]=x;
}

void dfs2(int x) {
    int i, y;
    viz[x] = 1;


    sol[p].push_back(x);
    //fout<<x<<" ";
    for (i = 0; i < w[x].size(); i++) {
        y = w[x][i];
        if (viz[y]==0)
            dfs2(y);
    }
}

int main()
{
    fin>>n>>m;
    for(i=1;i<=m;i++)
    {
        fin>>x>>y;
        v[x].push_back(y);
        w[y].push_back(x);
    }
    for(i=1;i<=n;i++)
    {
        if(viz[i]==0)
        {
            dfs(i);
        }
    }
    clean();
    p=0;
    for(i=k;i>=1;i--)
    {
        if(viz[postordine[i]] ==0)
        {
            //fout<<postordine[i];
            p++;
            b=0;
            dfs2(postordine[i]);
           // fout<<"\n";
           componente++;
        }
    }
    fout<<componente<<"\n";
    for(i=1;i<=componente;i++)
    {
        for(j=0;j<sol[i].size();j++)
        {
            fout<<sol[i][j]<<" ";
        }
        fout<<"\n";
    }

    for(i=k;i>=1;i--)
    {
       // fout<<postordine[i]<<" ";
    }
}