Cod sursa(job #2469036)

Utilizator Savu_Stefan_CatalinSavu Stefan Catalin Savu_Stefan_Catalin Data 6 octombrie 2019 14:08:30
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream in("ctc.in");
ofstream out("ctc.out");
vector <int> g[100001];
vector <int> G[100001];
vector <int> c[100001];
stack  <int> s;
int b[100001],n,m,i,nr,j,x,y,a;
void df(int x)
{
    b[x]=1;
    for (int i=0;i<g[x].size();++i)
    {
        if (!b[g[x][i]]) df(g[x][i]);
    }
    s.push(x);
}
void df2(int x)
{
    b[x]=2;
    c[nr].push_back(x);
    for (int i=0;i<G[x].size();++i)
    {
        if (b[G[x][i]]==1) df2(G[x][i]);
    }
}
int main()
{
 in>>n>>m;
 for (i=1;i<=m;++i)
{
    in>>x>>y;
    g[x].push_back(y);
    G[y].push_back(x);
}
for (i=1;i<=n;++i)
{
    if (!b[i])
        df(i);
}
while (!s.empty())
{
    a=s.top();
    s.pop();
    if (b[a]==1)
    {
        nr++;
        df2(a);
    }
}
out<<nr<<'\n';
for (i=1;i<=nr;++i)
{
    for (j=0;j<c[i].size();++j)
        out<<c[i][j]<<" ";
    out<<'\n';
}

    return 0;
}