Cod sursa(job #1568593)

Utilizator VladuZ1338Vlad Vlad VladuZ1338 Data 14 ianuarie 2016 15:07:02
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 kb
#include <bits/stdc++.h>

using namespace std;

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

vector <int> v[100001], vt[100001];
bitset <100001> viz;
stack <int> S, ST;

int x, n, m, i, xc, y, nr, j;
char parser [20];

void DFS (int x)
{
    viz[x]=1;
    for (vector <int> ::iterator it=v[x].begin(); it!=v[x].end(); ++it)
    {
        if (viz[*it]==0)
        {
            DFS (*it);
        }
    }
    S.push(x);
}

void DFST (int x)
{
    viz[x]=1;
    for (vector <int> ::iterator it=vt[x].begin(); it!=vt[x].end(); ++it)
    {
        if (viz[*it]==0)
        {
            DFST (*it);
        }
    }
    ST.push(x);
}

int main()
{
    fin >> n >> m; fin.get();
    for (i=1; i<=m; i++)
    {
        fin.getline (parser, 20);
        x=y=j=0;
        while (parser[j]!=' ')
        {
            x=x*10+parser[j]-'0';
            ++j;
        }
        ++j;
        while (parser[j])
        {
            y=y*10+parser[j]-'0';
            ++j;
        }
        v[x].push_back(y);
        vt[y].push_back(x);
    }
    for (i=1; i<=n; i++)
    {
        if (viz[i]==0) DFS (i);
    }
    viz.reset();
    while (!S.empty())
    {
        xc = S.top(); S.pop();
        if (viz[xc]==0)
        {
            ST.push (-1);
            nr++;
            DFST (xc);
        }
    }
    fout << nr << "\n";
    while (!ST.empty())
    {
        xc=ST.top();
        if(xc==-1) fout << "\n";
        else fout << xc << " ";
        ST.pop();
    }
}