Cod sursa(job #2256605)

Utilizator ralfd123Amariei Andrei ralfd123 Data 8 octombrie 2018 21:09:35
Problema Parcurgere DFS - componente conexe Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <fstream>
using namespace std;
ifstream f("dfs.in"); ofstream g("dfs.out");
int a[1010][1010],viz[1010],n,m,cc;

void citire()
{   f>>n>>m;int x=0,y=0;
    while(m)
    {   f>>x>>y; a[x][y]=a[y][x]=1;
        m--;
    }
}

void depth_first(int vf)
{   viz[vf]=1;
    for(int j=1;j<=n;j++)
        if( a[vf][j] == 1 and viz[j] == 0 ) depth_first(j);
}

void afis()
{   for(int i=1;i<=n;i++)
    {   for(int j=1;j<=n;j++) g<<a[i][j]<<" ";
        g<<'\n';
    }
}

int main()
{   citire();

    for(int i=1;i<=n;i++)
        if( viz[i] == 0 ) {depth_first(i); cc++;}

    g<<cc;

g.close();
return 0;
}