Cod sursa(job #1979173)

Utilizator GodSlayerTabara Emanuel GodSlayer Data 9 mai 2017 20:35:18
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <bits/stdc++.h>
using namespace std;
vector <int> G[100005];
int vec[100005];
ifstream in("dfs.in");
ofstream out("dfs.out");

int DFS(int nod)
{    vec[nod] = 1;
    int j, i;
    for(i = 0; i < G[nod].size(); i++)
     {
         if(vec[G[nod][i] ]!= 1)
         DFS(G[nod][i]);
     }
}



int main()
{
    int n, m;
    in >> n >> m;
    int j, nr = 0, k;
    for(int i = 0; i < m; i++)
        {
            in  >> j >> k;
            G[j].push_back(k);
            G[k].push_back(j);
        }
        for(int j = 1; j <= n; j++)
            if(vec[j] != 1)
        {
            DFS(j);
            nr++;
        }
    out << nr;
    return 0;
}