Cod sursa(job #3212713)

Utilizator Razvan23Razvan Mosanu Razvan23 Data 12 martie 2024 09:13:55
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>
using namespace std;

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

vector<int> L[100005], G[100005];
vector<int> C[100005];
int st[100005], n, m, l, nrc;
bitset<100005> v;

void DFS(int k)
{
    v[k] = 1;
    for(auto w : L[k])
        if(!v[w]) DFS(w);
    st[++l] = k;
}

void DFS1(int k)
{
    v[k] = 1;
    C[nrc].push_back(k);
    for(auto w : G[k])
        if(!v[w]) DFS1(w);
}

int main()
{
    ios_base::sync_with_stdio(0);
    fin.tie(0);
    fout.tie(0);
    int i, x, y;
    fin >> n >> m;
    for(i=1;i<=m;i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
        G[y].push_back(x);
    }
    for(i=1;i<=n;i++)
        if(!v[i]) DFS(i);
    v.reset();
    for(i=n;i>=1;i--)
        if(!v[st[i]])
        {
            nrc++;
            DFS1(st[i]);
        }
    fout << nrc << "\n";
    for(i=1;i<=nrc;i++)
    {
        sort(C[i].begin(), C[i].end());
        for(auto w : C[i])
            fout << w << " ";
        fout << "\n";
    }
    fin.close();
    fout.close();
    return 0;
}