Cod sursa(job #2002993)

Utilizator dumitrescu_andreiDumitrescu Andrei dumitrescu_andrei Data 21 iulie 2017 13:41:42
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>

using namespace std;

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

int n,m;
vector <int> V[100001];
vector <int> v[100001];
vector <int> dawaj[100001];
int ord[1000001];
int post,nr;
bool viz[100001];

void DFS(int x)
{
    viz[x]=1;
    for(int i=0;i<V[x].size();++i)
        if(!viz[V[x][i]])
           DFS(V[x][i]);
    ord[++post]=x;
}

void DFST(int x)
{
    viz[x]=0;
    for(int i=0;i<v[x].size();++i)
        if(viz[v[x][i]])
         DFST(v[x][i]);
    dawaj[nr].push_back(x);
}

int main()
{
 f>>n>>m;


 for(int i=1;i<=m;++i)
 {
     int x,y;
     f>>x>>y;
     V[x].push_back(y);
     v[y].push_back(x);
 }

 for(int i=1;i<=n;++i)
    if(!viz[i])
      DFS(i);

 for(int i=n;i>=1;--i)
    if(viz[ord[i]])
 {
     ++nr;
     DFST(ord[i]);
 }

g<<nr<<'\n';
for(int i=1;i<=nr;++i)
{
    for(int j=0;j<dawaj[i].size();++j)
        g<<dawaj[i][j]<<" ";
    g<<'\n';
}
return 0;
}