Cod sursa(job #1804072)

Utilizator stefan_gheorgheGheorghe Stefan stefan_gheorghe Data 12 noiembrie 2016 10:45:21
Problema Parcurgere DFS - componente conexe Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.44 kb
#include <fstream>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n,m,i,j,x,y,a[10001][10001];
bool sel[100001];
void load()
{
    f>>n>>m;
    for (i=1; i<=m; i++)f>>x>>y,a[x][y]=1,a[y][x]=1;
}
void DF(int x)
{int i;
    sel[x]=true;
    for (i=1;i<=n;i++)if (a[x][i]==1 && !sel[i]) DF(i);
}
int main()
{
    load();
    int nr=0;
    for (i=1;i<=n;i++)if (!sel[i])nr++,DF(i);
    g<<nr;
    return 0;
}