Cod sursa(job #2050713)

Utilizator Gl0WCula Stefan Gl0W Data 28 octombrie 2017 11:06:50
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream fin ("dfs.in");
ofstream fout ("dfs.out");

vector<int> l[100005];
int viz[100005];
int n, m, x, y, nr;
void dfs(int nod){
    int i;
    viz[nod] = 1;
    for(i = 0; i < l[nod].size(); i++){
        if(viz[l[nod][i]] == 0){
            dfs(l[nod][i]);
        }
    }
}

int main()
{
    fin>>n>>m;
    for(int i = 1; i <= m; i++){
        fin>>x>>y;
        l[x].push_back(y);
        l[y].push_back(x);
    }
    for(int i = 1; i <= n; i++){
        if(viz[i] == 0){
            dfs(i);
            nr++;
        }
    }
    fout<<nr;
    return 0;
}