Cod sursa(job #901066)

Utilizator RaduDoStochitoiu Radu RaduDo Data 28 februarie 2013 23:58:41
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<bitset>
#define INF 0x3f3f3f3f
#define mp make_pair
#define pb push_back
#define maxn 100010
using namespace std;
bitset < maxn > sel;
vector < int > G[maxn],GT[maxn],St;
int x,y,n,m,i,nr;

void DF(int x)
{
    sel[x]=1;
    for(int i=0; i<G[x].size(); ++i)
        if(!sel[G[x][i]])
            DF(G[x][i]);
    St.pb(x);
}

void DFT(int x, bool k)
{
    if(k) printf("%d ",x);
    sel[x]=1;
    for(int i=0; i<GT[x].size(); ++i)
        if(!sel[GT[x][i]])
        {
            DFT(GT[x][i], k);
        }
}

int main()
{
    freopen("ctc.in","r",stdin);
    freopen("ctc.out","w",stdout);
    scanf("%d%d",&n,&m);
    while(m--)
        scanf("%d%d",&x,&y),G[x].pb(y),GT[y].pb(x);
    for(i=1; i<=n; ++i)
        if(!sel[i])
            DF(i);
    sel.reset();
    for(i=n; i>0; --i)
        if(!sel[St[i-1]])
            DFT(St[i-1],false),nr++;
    sel.reset();
    printf("%d\n",nr);
    for(i=n; i>0; --i)
        if(!sel[St[i-1]])
            DFT(St[i-1],true),printf("\n");
    return 0;
}