Cod sursa(job #1689325)

Utilizator crysstyanIacob Paul Cristian crysstyan Data 14 aprilie 2016 09:57:35
Problema Componente tare conexe Scor 34
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#define NMAX 100005

using namespace std;

ifstream f("ctc.in");
ofstream g("ctc.out");

int i, n, m, x, y, start[NMAX], cnt = 0, cntans = 0;
vector < int > v[NMAX], vt[NMAX], ans[NMAX];
bool visited[NMAX], used[NMAX];

void DFS1(int node)
{
    visited[node] = 1;

    for (auto & it : v[node])
        if (!visited[it])
            DFS1(it);
}

void DFS2(int node)
{
    visited[node] = 0;
    ans[cntans].push_back(node);

    for (auto & it : vt[node])
        if (visited[it])
        {
            used[it] = 1;
            DFS2(it);
        }
}

int main()
{
    f >> n >> m;

    for (i = 1; i <= m; ++ i)
    {
        f >> x >> y;
        v[x].push_back(y);
        vt[y].push_back(x);
    }

    for (i = 1; i <= n; ++ i)
        if (!used[i])
    {
        DFS1(i);
        cntans ++;
        DFS2(i);
    }
    g << cntans << '\n';

    for (i = 1; i <= cntans; ++ i)
    {
        for (auto & it : ans[i])
            g << it << " ";
        g << '\n';
    }
    return 0;
}