Cod sursa(job #2661197)

Utilizator alexia208160Popescu Alexia Maria alexia208160 Data 21 octombrie 2020 16:18:34
Problema Parcurgere DFS - componente conexe Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <vector>

using namespace std;

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

vector <int> v[100000];
bool seen[100000];

void dfs(int x)
{
    for(int i = 0; i < v[x].size(); i++)
    {
        if(seen[v[x][i]] == 0)
        {
            seen[v[x][i]] = 1;
            dfs(v[x][i]);
        }
    }
}

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