Cod sursa(job #2357522)

Utilizator triscacezarTrisca Vicol Cezar triscacezar Data 27 februarie 2019 15:14:31
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>

using namespace std;

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

int n,m,i,k,depth[100010],up[100010];
vector<int> v[100010];
bitset<100010> in,viz;
stack<int> St;
vector<vector<int> > C;

void dfs(int poz)
{
    viz[poz]=1;
    St.push(poz);
    in[poz]=1;
    for(auto it:v[poz])
        if(!viz[it])
        {
            depth[it]=up[it]=++k;
            dfs(it);
            up[poz]=min(up[poz],up[it]);
        }
        else
            if(in[it])
                up[poz]=min(up[poz],depth[it]);
    if(up[poz]==depth[poz])
    {
        vector<int> c;
        while(St.top()!=poz)
        {
            in[St.top()]=0;
            c.push_back(St.top());
            St.pop();
        }
        in[poz]=0;
        c.push_back(poz);
        St.pop();
        C.push_back(c);
    }
}

int main()
{
    f>>n>>m;
    for(i=1;i<=m;i++)
    {
        int x,y;
        f>>x>>y;
        v[x].push_back(y);
    }
    for(i=1;i<=n;i++)
        if(!viz[i])
        {
            depth[i]=up[i]=++k;
            dfs(i);
        }
    g<<C.size()<<'\n';
    for(auto it:C)
    {
        sort(it.begin(),it.end());
        for(auto that:it)
            g<<that<<' ';
        g<<'\n';
    }
    return 0;
}