Cod sursa(job #2790317)

Utilizator DordeDorde Matei Dorde Data 28 octombrie 2021 19:39:56
Problema Componente tare conexe Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f ("ctc.in");
ofstream g ("ctc.out");
int const N = 1e5 + 3;
int stop [N];
vector <int> v [N] , v2 [N] , comp;
vector <vector <int> > sol;
bool viz [N];
void dfs (int node){
    viz [node] = true;
    for(auto y : v [node])
        if (! viz [y])
            dfs (y);
    stop [++ stop [0]] = node;
}
void dfs2 (int node){
    viz [node] = true;
    comp.push_back (node);
    for(auto y : v2 [node])
        if (! viz [y])
            dfs2 (y);
}
int main()
{
    int n , m;
    f >> n >> m;
    for(int i = 1 ; i <= m ; ++ i){
        int a , b;
        f >> a >> b;
        v [a].push_back (b);
        v2 [b].push_back (a);
    }
    for(int i = 1 ; i <= n ; ++ i)
        if (! viz [i])
            dfs (i);
    fill (viz , viz + n + 1 , false);
    for(int i = stop [0] ; i ; -- i)
        if (! viz [stop [i]]){
            comp.clear ();
            dfs2 (stop [i]);
            sol.push_back (comp);
        }
    g << sol.size () << '\n';
    for(auto y : sol){
        for(auto y2 : y)
            g << y2 << ' ';
        g << '\n';
    }
    return 0;
}