Cod sursa(job #3206232)

Utilizator vvvvvvvvvvvvvVusc David vvvvvvvvvvvvv Data 22 februarie 2024 00:01:54
Problema Componente tare conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <fstream>
#include <stack>
#include <vector>

using namespace std;

ifstream fin("ctc.in");
ofstream fout("ctc.out");
const int NMAX = 100001;
int n, m, v[NMAX], v2[NMAX], j = 0;
vector<int> g[NMAX], g1[NMAX];
vector<int> sol[NMAX];
stack<int> s;

void dfs(int k){
    v[k] = 1;
    s.push(k);
    for(int i : g[k]) if(!v[i]) dfs(i);
}

void dfs2(int k){
    v2[k] = 1;
    for(int i : g1[k]) if(!v2[i]) dfs2(i);
    sol[j].push_back(k);
}

int main(){
    fin >> n >> m;
    for(int i = 1; i <= m; i++){
        int x, y;
        fin >> x >> y;
        g[x].push_back(y);
        g1[y].push_back(x);
    }
    for(int i = 1; i <= n; i++) if(!v[i]) dfs(i);
    while (!s.empty())
    {
        if(!v2[s.top()]) dfs2(s.top()), j++;
        s.pop();
    }
    fout << j << '\n';
    for(int i = 0; i < j; i++){
        for(auto e : sol[i]) fout << e << ' ';
        fout << '\n';
    }
    return 0;
}