Cod sursa(job #1568585)

Utilizator VladuZ1338Vlad Vlad VladuZ1338 Data 14 ianuarie 2016 14:59:33
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 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;

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;
    for (i=1; i<=m; i++)
    {
        fin >> x >> y;
        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++;
            //fout << "CTC" << nr << ": ";
            DFST (xc);
            //fout << "\n";
        }
    }
    fout << nr << "\n";
    while (!ST.empty())
    {
        xc=ST.top();
        if(xc==-1) fout << "\n";
        else fout << xc << " ";
        ST.pop();
    }
}