Cod sursa(job #2256626)

Utilizator ralfd123Amariei Andrei ralfd123 Data 8 octombrie 2018 21:34:27
Problema Parcurgere DFS - componente conexe Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 kb
#include <fstream>
using namespace std;
ifstream f("dfs.in"); ofstream g("dfs.out");
int viz[100010],n,m,cc;
struct muchii{int x,y;}v[200010];

void citire()
{   f>>n>>m;
    for(int i=1;i<=m;++i) f>>v[i].x>>v[i].y;
}

void depth_first(int x)
{   viz[x]=1;
    for(int i=1;i<=m;i++)
        if( x == v[i].x and viz[v[i].y] == 0 ) depth_first(v[i].y);
}

int main()
{   citire();

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

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