Cod sursa(job #1988010)

Utilizator flaviu_2001Craciun Ioan-Flaviu flaviu_2001 Data 1 iunie 2017 20:35:21
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <fstream>
#include <vector>

using namespace std;

int n, m, ordine[100005], no;
vector<int> v[100005], vt[100005], r;
vector< vector<int> > sol;
bool vis[100005];

void dfs(int x)
{
    vis[x] = 1;
    for (unsigned i = 0; i < v[x].size(); ++i)
        if(!vis[v[x][i]])
            dfs(v[x][i]);
    ordine[++no] = x;
}

void dfst(int x)
{
    vis[x] = 0;
    r.push_back(x);
    for (unsigned i = 0; i < vt[x].size(); ++i)
        if(vis[vt[x][i]])
            dfst(vt[x][i]);
}

int main()
{
    ifstream fin ("ctc.in");
    ofstream fout ("ctc.out");
    fin >> n >> m;
    while(m--){
        int x, y;
        fin >> x >> y;
        v[x].push_back(y);
        vt[y].push_back(x);
    }
    for (int i = 1; i <= n; ++i)
        if(!vis[i])
            dfs(i);
    for (int i = n; i; --i)
        if(vis[ordine[i]]){
            r.clear();
            dfst(ordine[i]);
            sol.push_back(r);
        }
    fout << sol.size() << "\n";
    for (unsigned i = 0; i < sol.size(); ++i){
        for (unsigned j = 0; j < sol[i].size(); ++j)
            fout << sol[i][j] << " ";
        fout << "\n";
    }
    fin.close();
    fout.close();
    return 0;
}