Cod sursa(job #2174048)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 16 martie 2018 10:33:20
Problema Componente tare conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <bits/stdc++.h>
#define Nmax 100001
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
list <int> G[Nmax];
int n,m,N,ans,node;
list <int> ctc[Nmax];
int dfn[Nmax];
int low[Nmax];
stack <int> st;
void DFS(int x)
{
    dfn[x]=low[x]=++N;
    st.push(x);
    for(const auto &it:G[x])
        {
            if(dfn[it]==-1) DFS(it);
            low[x]=min(low[x],low[it]);
        }
    if(low[x]==dfn[x])
    {
        ++ans;
        do
        {
            node=st.top();
            ctc[ans].push_back(node);
            st.pop();
        }while(node!=x);
    }
}
int main()
{
    int i,j;
    f>>n>>m;
    while(m--)
    {
        f>>i>>j;
        G[i].push_back(j);
    }
    fill(dfn+1,dfn+n+1,-1);
    fill(low+1,low+n+1,-1);
    DFS(1);
    g<<ans<<'\n';
    for(i=1;i<=n;i++,g<<'\n')
        for(const auto &it:ctc[i])
            g<<it<<' ';

    return 0;
}