Cod sursa(job #2438217)

Utilizator mihaihernestHernest Mihai mihaihernest Data 11 iulie 2019 17:33:41
Problema Parcurgere DFS - componente conexe Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n,x,y,a[102][102],nrc,v1[102],viz[102],i,j,m;
void comp(int x)
{
    int i;
    viz[x] = 1;
    for(i = 1 ; i <= a[x][0]; ++i)
    if(viz[a[x][i]] == 0)
        comp(a[x][i]);

}
int main()
{
    f >> n >> m;
    for(i = 1 ; i <= m ; ++i)
    {   f >> x >> y;
        a[x][0]++;
        a[x][a[x][0]] = y;
        a[y][0]++;
        a[y][a[y][0]] = x;
    }
    for(i = 1 ; i <= n ; ++i)
    if(viz[i] == 0){
        nrc++;
        v1[nrc] = i;
        comp(i);
    }
    g << nrc;

    return 0;
}