Cod sursa(job #2124846)

Utilizator ccsgeorge1Cocis George ccsgeorge1 Data 7 februarie 2018 17:40:16
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <vector>
#include <fstream>
#define limit 100001

using namespace std;

vector <int > v[limit];
bool viz[limit];
int m, n, conexe=0;

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

int main()
{
    ifstream fin ("test.in");
    ofstream fout ("test.out");

    fin>>n >>m;

    for (int i = 1; i <= m; i++)
    {
        int x, y;
        fin>>x >>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }

    for (int i = 1; i <= n; i++)
        if (!viz[i])
        {
            conexe++;
            dfs(i);
        }
    fout<<conexe;
    return 0;
}