Cod sursa(job #1807649)

Utilizator serbanSlincu Serban serban Data 16 noiembrie 2016 19:46:56
Problema Componente tare conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <fstream>
#include <vector>

using namespace std;

vector<int> L[100005];
vector<int> G[100005];
int sol[100005];

int k;
vector<int> s[100005];

void df(int x) {
    sol[x] = 1;
    for(int i = 0; i < L[x].size(); i ++) {
        if(!sol[L[x][i]])
            df(L[x][i]);
    }
}

void dfT(int x) {
    sol[x] = 2;
    for(int i = 0; i < G[x].size(); i ++) {
        if(sol[G[x][i]] == 1)
            dfT(G[x][i]);
    }
}

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

    int n, m, x, y;
    f >> n >> m;
    for(int i = 1; i <= m; i ++) {
        f >> x >> y;
        L[x].push_back(y);
        G[y].push_back(x);
    }

    for(int i = 1; i <= n; i ++) {
        if(!sol[i]) {
            k ++;
            df(i);
            dfT(i);

            for(int j = 1; j <= n; j ++) {
                if(sol[j] == 2) {
                    s[k].push_back(j);
                    sol[j] = 3;
                }
                else sol[j] = 0;
            }
        }
    }

    g << k << "\n";
    for(int i = 1; i <= k; i ++) {
        for(int j = 0; j < s[i].size(); j ++)
            g << s[i][j] << " ";
        g << "\n";
    }
    return 0;
}