Cod sursa(job #2439543)

Utilizator StorakNo Name Storak Data 16 iulie 2019 11:58:53
Problema Componente tare conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("ctc.in");
ofstream out("ctc.out");
const int N = 1e5+5;
vector<int> v[N],vt[N],ans[N];
stack<int> ord;
bool viz[N];
int k;
void dfp(int x)
{
    viz[x] = 1;
    for (auto it: v[x])
        if (!viz[it])
            dfp(it);
    ord.push(x);
}
void dfm(int x)
{
    viz[x] = 1;
    ans[k].push_back(x);
    for (auto it: vt[x])
        if (!viz[it])
            dfm(it);
}
int main()
{
    int n,m;
    in >> n >> m;
    for (int i = 1; i<=m; i++)
    {
        int x,y;
        in >> x >> y;
        v[x].push_back(y);
        vt[y].push_back(x);
    }
    dfp(1);
    memset(viz,0,sizeof(viz));
    while (!ord.empty())
    {
        int now = ord.top();
        ord.pop();
        if (!viz[now])
        {
            k++;
            dfm(now);
        }
    }
    out << k << "\n";
    for (int i = 1; i<=k; i++)
    {
        for (auto it: ans[i])
            out << it << " ";
        out << "\n";
    }
}