Cod sursa(job #146530)

Utilizator igorPirnau Igor igor Data 1 martie 2008 20:58:57
Problema Parcurgere DFS - componente conexe Scor 5
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include<fstream.h>

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

#define nmax 100100
#define mmax 400500

int n, m, a[mmax], st[nmax], viz[nmax], i, sum, nr;
 
void DFS(int p)
{
    int j;
    viz[p]=1;
    for(j=st[p]+1; j<=st[p+1]; j++) if(!viz[a[j]]) DFS(a[j]);
}

int main()
{
    int x, y;

    f>>n>>m;
    for(i=1; i<=m; i++){
        f>>x>>y;
        st[x]++; st[y]++;
    }
    f.close();
    
    sum=0;
    for(i=1; i<=n+2; i++){
        st[i-1]=sum;
        sum=sum+st[i];
    }

    ifstream f("dfs.in");
    f>>n>>m;
    for(i=1; i<=m; i++){
        f>>x>>y;
        a[st[x]]=y;
        a[st[y]]=x;
        st[x]--;
        st[y]--;
    }
    f.close();

    for(i=1; i<=n; i++){
        if(!viz[i]) DFS(i);
        nr++;
    }

    g<<nr;
    g.close();
    return 0;
}