Cod sursa(job #2145414)

Utilizator tanasaradutanasaradu tanasaradu Data 27 februarie 2018 12:46:26
Problema Componente tare conexe Scor 90
Compilator cpp 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 Nmax =  100005;
vector < int > L[Nmax] , H[Nmax] , S[Nmax];
int n , m , tmp[Nmax] , k , cc;
bool viz[Nmax];
void DFS_Sort(int nod)
{
    viz[nod] = true;
    for(auto i : L[nod])
        if(!viz[i])
            DFS_Sort(i);
    ++k;
    tmp[k] = nod;
}
void DFS_Solve(int nod)
{
    viz[nod] = true;
    for(auto i : H[nod])
        if(!viz[i])
            DFS_Solve(i);
    S[cc] . push_back(nod);
}
int main()
{
    int x , y;
    fin >> n >> m;
    for(int i = 1 ; i <= m ; i++)
    {
        fin >> x >> y;
        L[x] . push_back(y);
        H[y] . push_back(x);
    }
    for(int i = 1 ; i <= n ; i++)
        if(!viz[i])
            DFS_Sort(i);
    for(int i = 1 ; i <= n ; i++)
        viz[i] = false;

    for(int i = k ; i >= 1 ; i--)
        if(!viz[tmp[i]])
    {
        ++cc;
        DFS_Solve(tmp[i]);
    }
    fout << cc << "\n";
    for(int i = 1 ; i <= cc ; i++)
    {
        for(auto j : S[i])
            fout << j << " ";
        fout << "\n";
    }
    fin.close();
    fout.close();
    return 0;
}