Cod sursa(job #2863753)

Utilizator Maftei_RazvanMaftei Ravzan Maftei_Razvan Data 7 martie 2022 10:16:58
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <bits/stdc++.h>
#define cal 100005
using namespace std;
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
int n, m, x, y, cnt;
vector <int> g[cal], gt[cal];
set <int> r[cal];
vector <bool> v;
vector <int> s;
void dfs(int x)
{
    v[x]=true;
    for(auto h : g[x])
    {
        if(v[h]==0)
        {
            dfs(h);
        }
    }
    s.push_back(x);
}
void dfst(int x)
{
    v[x]=1;
    r[cnt].insert(x);
    for(auto h : gt[x])
    {
        if(!v[h])
        {
            dfst(h);
        }
    }
}
int main()
{
    fin >> n >> m;
    for(int i=1; i<=m; i++)
    {
        fin >> x >> y;
        g[x].push_back(y);
        gt[y].push_back(x);
    }
    v=vector <bool> (n+1, false);
    for(int i=1; i<=n; i++)
    {
        if(v[i]==0)
        {
            dfs(i);
        }
    }
    v=vector <bool> (n+1, false);
    reverse(s.begin(), s.end());
    for(auto h : s)
    {
        if(v[h]==0)
        {
            cnt++;
            dfst(h);
        }
    }
    fout << cnt << "\n";
    for(int i=1; i<=cnt; i++)
    {
        for(auto h : r[i])
        {
            fout << h << " ";
        }
        fout << "\n";
    }
    return 0;
}