Cod sursa(job #1374916)

Utilizator IonutLLuca Ionut IonutL Data 5 martie 2015 11:23:43
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");

int N, M, x, y, sol;
vector < int > G[100005], T[100005], CTC[100005], srt;
bitset < 100005 > viz;

void dfs(int nod)
{
    viz[nod]=1;
    vector<int>::iterator it=G[nod].begin();
    for (; it!=G[nod].end(); ++it)
        if (!viz[*it]) dfs(*it);
    srt.push_back(nod);
}

void dfst(int nod)
{
    viz[nod]=0; CTC[sol].push_back(nod);
    vector<int>::iterator it=T[nod].begin();
    for (; it!=T[nod].end(); ++it)
        if (viz[*it]) dfst(*it);
}

int main()
{
    f>>N>>M;
    for (int i=1; i<=M; ++i)
    {
        f>>x>>y;
        G[x].push_back(y);
        T[y].push_back(x);
    }

    for (int i=1; i<=N; ++i)
        if (!viz[i]) dfs(i);

    for (int i=srt.size()-1; i>=0; --i)
        if (viz[srt[i]])
            ++sol, dfst(srt[i]);

    g<<sol<<'\n';
    for (int i=1; i<=sol; ++i && g<<'\n')
        for (int j=0; j<(int)CTC[i].size(); ++j)
            g<<CTC[i][j]<<' ';
    return 0;
}