Cod sursa(job #3340010)

Utilizator andrei1232008nicolae andrei andrei1232008 Data 11 februarie 2026 16:37:15
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>

using namespace std;

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

const int lim=3e5+10;
int i,n,m,comp;
bool viz[lim];
vector <int> v[lim],g[lim];
vector <int> a;
set <int> s[lim];
void dfs_scc(int k)
{
    viz[k]=1;
    for(auto x:v[k])
    {
        if(viz[x]==0)
            dfs_scc(x);
    }
    a.push_back(k);
}

void clear_viz(int n)
{
    int i;
    for(i=1;i<=n;i++)
        viz[i]=0;
}

void components(int k)
{
    viz[k]=1;
    s[comp].insert(k);
    for(auto x:g[k])
    {
        if(viz[x]==0)
            components(x);
    }
}
int main()
{
    fin>>n>>m;
    for(i=1;i<=m;i++)
    {
        int x,y;
        fin>>x>>y;
        v[x].push_back(y);
        g[y].push_back(x);
    }
    for(i=1;i<=n;i++)
        if(viz[i]==0)dfs_scc(i);

    clear_viz(n);
    reverse(a.begin(),a.end());
    for(auto x:a)
    {
        if(viz[x]==0)
            {
                comp++;
                components(x);
            }
    }
    fout<<comp<<'\n';
    for(i=1;i<=comp;i++)
    {
        for(auto x:s[i])
            fout<<x<<" ";
        fout<<'\n';
    }
    return 0;
}