Cod sursa(job #2886259)

Utilizator bogdan.svai2004@gmail.comSvaicovschi Bogdan-Gabriel [email protected] Data 7 aprilie 2022 15:05:46
Problema Componente tare conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <fstream>
#include <vector>
#include <bitset>
std :: ifstream in("ctc.in");
std :: ofstream out("ctc.out");
using namespace std;

const int C= 1000001;

vector <int> succ[C], predec[C];
vector <int> ctc[C];
vector <int> s;
bitset <C> seen;
int n, nc;

void dfs(int x)
{
    seen[x] = true;
    for (auto y: succ[x])
    {
        if (!seen[y])
        {
            dfs(y);
        }
    }
    s.push_back(x);
}

void dfs_transpus(int x)
{
    ctc[nc].push_back(x);
    viz[x] = 1;
    for (auto y: predec[x])
    {
        if (!viz[y])
        {
            dfs_t(y);
        }
    }
}

int main()
{
    
    int m;
    in >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int x, y;
        in >> x >> y;
        succ[x].push_back(y);
        predec[y].push_back(x);
    }
    in.close();
    for (int i = 1; i <= n; i++)
    {
        if (!viz[i])
        {
            dfs(i);
        }
    }
   for(int i=0;i<=n;i++) viz[i]=false;
    for (int i = (int)s.size() - 1; i >= 0; i--)
    {
        if (!viz[s[i]])
        {
            nc++;
            dfs_transpus(s[i]);
        }
    }
    out << nc << "\n";
    for (int i = 1; i <= nc; i++)
    {
        for (auto x: ctc[i])
        {
            out << x << " ";
        }
        out << "\n";
    }
    out.close();
    return 0;
}