Cod sursa(job #381565)

Utilizator cristikIvan Cristian cristik Data 10 ianuarie 2010 22:42:18
Problema Componente tare conexe Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <stdio.h>
#include <vector>
#define max 100010
using namespace std;
vector<int> g[max];
int n,m,stack[max],top,stack2[max],I[max],top2,c,i,j;
void dfs(int v)//gabow
{
    stack[++top]=v; I[v]=top; stack2[++top2]=I[v];
    for(int i=0; i<g[v].size(); i++)
     if(I[g[v][i]]==0) dfs(g[v][i]);
     else while(I[g[v][i]]<stack2[top2]) top2--;
    if(I[v]==stack2[top2])
    {
        top2--;
        c++;
        while(I[v]<=top) I[stack[top--]]=c;
    }
}
int main()
{
    freopen("ctc.in","r",stdin);
    freopen("ctc.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(; m>0; m--)
    {
        scanf("%d%d",&i,&j);
        g[i].push_back(j);
    }
    c=n;
    for(i=1; i<=n; i++)
     if(!I[i]) dfs(i);
    printf("%d\n",c-n);
    for(i=n+1; i<=c; i++)
    {
        for(j=1; j<=n; j++)
         if(I[j]==i) printf("%d ",j);
        printf("\n");
    }
    return 0;
}