Mai intai trebuie sa te autentifici.

Cod sursa(job #1707072)

Utilizator danstefanDamian Dan Stefan danstefan Data 24 mai 2016 09:48:24
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>
using namespace std;
int st[100010],i,n,m,x,y,nc,k;
bool sel[100010];
vector<int>g[100010],g2[100010];
void DFS1(int x)
{
    int i;
    sel[x]=true;
    for(i=0; i<g[x].size(); ++i)
        if(!sel[g[x][i]])DFS1(g[x][i]);
    st[++k]=x;
}
void DFS2(int x,bool k)
{
    int i;
    if(k)printf("%d ",x);
    sel[x]=true;
    for(i=0; i<g2[x].size(); ++i)
        if(!sel[g2[x][i]])DFS2(g2[x][i],k);
}
int main()
{
    freopen("ctc.in","r",stdin);
    freopen("ctc.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(i=1; i<=m; ++i)
    {
        scanf("%d%d",&x,&y);
        g[x].push_back(y);
        g2[y].push_back(x);
    }
    for(i=1; i<=n; ++i)
        if(!sel[i])DFS1(i);
    for(i=1; i<=n; ++i)
        sel[i]=false;
    for(i=k; i>=1; --i)
        if(!sel[st[i]])
        {
            DFS2(st[i],false);
            ++nc;
        }
            printf("%d\n",nc);
            for(i=1; i<=n; ++i)
        sel[i]=false;
            for(i=k; i>=1; --i)
        if(!sel[st[i]])
        {
            DFS2(st[i],true);
            printf("\n");
        }
    return 0;
}