Cod sursa(job #2444372)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 31 iulie 2019 13:06:59
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <bits/stdc++.h>

using namespace std;

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

vector <int> a[100005], r, aT[100005], ans[100005];
int f[100005], anss;

void dfs(int k)
{
    f[k] = 1;
    for(auto v : a[k])
        if(!f[v]) dfs(v);
    r.push_back(k);
}

void dfsT(int k)
{
    ans[anss].push_back(k);
    f[k] = 1;
    for(auto v : aT[k])
        if(!f[v]) dfsT(v);
}
int main()
{
    ios::sync_with_stdio(false);
    fin.tie(0);
    int n, m;
    fin >> n >> m;
    for(int i = 1; i <= m; ++i) {
        int x, y;
        fin >> x >> y;
        a[x].push_back(y);
        aT[y].push_back(x);
    }
    for(int i = 1; i <= n; ++i)
        if(!f[i]) dfs(i);
    reverse(r.begin(), r.end());
    memset(f, 0 , sizeof(f));
    for(auto v : r) {
        if(!f[v]) {
            ++anss;
            dfsT(v);
        }
    }
    fout << anss << "\n";
    for(int i = 1; i <= anss; ++i) {
        for(auto v : ans[i]) fout << v << " ";
        fout << "\n";
    }
    return 0;
}