Cod sursa(job #3225488)

Utilizator maiaauUngureanu Maia maiaau Data 17 aprilie 2024 18:39:08
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;
using ll = long long;
#define pb push_back

ifstream fin("ctc.in");
ofstream fout("ctc.out");

const int N = 1e5+3;

int n, k;
bitset<N> viz;
stack<int> stk;
vector<int> g[N], gt[N];
vector<vector<int>> ctc;

void read(), dfs(int), dfsgt(int);

int main()
{
    read();
    for (int i = 1; i <= n; i++)
        if (!viz[i])
            dfs(i);
    viz.reset();
    while (!stk.empty()){
        int nod = stk.top(); stk.pop();
        if (!viz[nod]){
            ctc.pb({});
            dfsgt(nod);
            k++;
        }
    }
    fout << k << '\n';
    for (auto it: ctc){
        for (auto el: it) fout << el << ' ';
        fout << '\n';
    }

    return 0;
}
void read(){
    int m; fin >> n >> m;
    while (m--){
        int a, b; fin >> a >> b;
        g[a].pb(b); gt[b].pb(a);
    }
}
void dfs(int nod){
    viz[nod] = 1;
    for (auto it: g[nod])
        if (!viz[it])
            dfs(it);
    stk.push(nod);
}
void dfsgt(int nod){
    viz[nod] = 1;
    ctc[k].pb(nod);
    for (auto it: gt[nod])
        if (!viz[it])
            dfsgt(it);
}


//18:31