Cod sursa(job #2002986)

Utilizator dumitrescu_andreiDumitrescu Andrei dumitrescu_andrei Data 21 iulie 2017 13:25:01
Problema Componente tare conexe Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 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];
int ord[1000001];
int post,nr;
int viz[100001];

void DFS(int x)
{
    viz[x]=0;
    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]=nr;
    for(int i=0;i<v[x].size();++i)
        if(!viz[v[x][i]])
         DFST(v[x][i]);

}

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

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

 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=1;j<=n;++j)
        if(viz[j]==i)
        g<<j<<" ";
    g<<'\n';
}

}