Cod sursa(job #2754359)

Utilizator AndreiDeltaBalanici Andrei Daniel AndreiDelta Data 25 mai 2021 18:05:35
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
typedef long long ll;
typedef pair<int,int> pi;
int t,T;
int n,m,cnt;
const int dim=1e5+10;
vector < int > V[dim],VT[dim],A[dim],ans;
vector < bool > viz(dim,0);

void DFS(int nod)
{
    viz[nod]=1;
    for(int x:V[nod])
    if(!viz[x]) DFS(x);
    ans.pb(nod);
}

void DFST(int nod){

    viz[nod]=1;
    for(int x:VT[nod])
    if(!viz[x]) DFST(x);
    A[cnt].pb(nod);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    f>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int a,b;
        f>>a>>b;
        V[a].pb(b);
        VT[b].pb(a);
    }

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

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

    reverse(ans.begin(),ans.end());
    for(int x:ans)
    if(!viz[x])
    {
        DFST(x);
        cnt++;
    }

    g<<cnt<<'\n';
    for(int i=0;i<cnt;i++)
    {
        for(int j:A[i]) g<<j<<' ';
        g<<'\n';
    }

    return 0;
}