Cod sursa(job #2481018)

Utilizator IoanaDraganescuIoana Draganescu IoanaDraganescu Data 26 octombrie 2019 12:55:24
Problema Componente tare conexe Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

const int nmax = 100000;

vector <int> g1[nmax + 5], g2[nmax + 5];
int n, m, x = 1, use[nmax + 5];

void Read(){
    fin >> n >> m;
    while (m--){
        int x, y;
        fin >> x >> y;
        g1[x].push_back(y);
        g2[y].push_back(x);
    }
}

void DFS1(int nod, int k){
    use[nod] = k;
    for (auto i : g1[nod])
        if (use[i] >= 0 && use[i] != k)
            DFS1(i, k);
}

void DFS2(int nod, int k){
    use[nod] = k;
    for (auto i : g2[nod])
        if (use[i] == -k && use[i] != k)
            DFS2(i, k);
}

void Solve(){
    for (int i = 1; i <= n; i++)
        if (use[i] >= 0){
            DFS1(i, x);
            DFS2(i, -x);
            x++;
        }
}

void Print(){
    fout << x - 1 << '\n';
    for (int i = 1; i < x; i++){
        for (int j = 1; j <= n; j++)
            if (use[j] == -i)
                fout << j << ' ';
        fout << '\n';
    }
}

int main(){
    Read();
    Solve();
    Print();
    return 0;
}