Cod sursa(job #2324650)

Utilizator AndreiLunguLungu Andrei Sebastian AndreiLungu Data 21 ianuarie 2019 11:46:10
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>
#define nmax 100004
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
int n , m , nrc;
bitset <nmax> viz;
vector <int> L[nmax];
vector <int> T[nmax];
vector <int> sol[nmax];
stack <int> st;
void Citire()
{
    int i , x , y;
    fin >> n >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
        T[y].push_back(x);
    }
    fin.close();
}
void DFS1(int nod)
{
    viz[nod] = 1;
    for(auto i : L[nod])
        if(viz[i] == 0)
            DFS1(i);
    st.push(nod);

}
void DFS2(int nod)
{
    viz[nod] = 1;
    sol[nrc].push_back(nod);
    st.push(nod);
    for(auto i : T[nod])
        if(viz[i] == 0)
            DFS2(i);

}
void Rezolvare()
{
    int i , k;
    for(i = 1; i <= n; i++)
        if(viz[i] == 0)
            DFS1(i);
    viz.reset();
    nrc = 0;
    while(!st.empty())
    {
        k = st.top();
        st.pop();
        if(viz[k] == 0)
        {
            nrc++;
            DFS2(k);
        }
    }
    fout << nrc << "\n";
    for(i = 1; i <= nrc; i++)
    {
        for(auto j : sol[i])
            fout << j << " ";
        fout << "\n";
        }
}
int main()
{
    Citire();
    Rezolvare();
    return 0;
}