Cod sursa(job #2299789)

Utilizator Victor_InczeVictor Incze Victor_Incze Data 10 decembrie 2018 09:16:44
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <bits/stdc++.h>
#define MAXN 100005

using namespace std;

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

bitset <MAXN> p;
int n, m, x, y, nr;
vector <int> sol[MAXN], g[MAXN], nug[MAXN];
deque <int> dq;

void dfs(int x)
{
    p[x]=1;
    for (int i=0; i<g[x].size(); i++)
        if (!p[g[x][i]])
            dfs(g[x][i]);
    dq.push_front(x);
}

void nudfs(int x)
{
    p[x]=0;
    for (int i=0; i<nug[x].size(); i++)
        if (p[nug[x][i]])
            nudfs(nug[x][i]);
    sol[nr].push_back(x);
}

void solvarea()
{
    for (int i=1; i<=n; i++)
        if (!p[i])
            dfs(i);
    while (!dq.empty())
    {
        if (p[dq.front()])
        {
            nr++;
            nudfs(dq.front());
        }
        dq.pop_front();
    }
}

int main()
{
    in >> n >> m;
    for (int i=1; i<=m; i++)
    {
        in >> x >> y;
        g[x].push_back(y);
        nug[y].push_back(x);
    }
    solvarea();
    out << nr << '\n';
    for (int i=1; i<=nr; i++)
    {
        for (int j=0; j<sol[i].size(); j++)
            out << sol[i][j] << " ";
        out << '\n';
    }
    return 0;
}