Cod sursa(job #2363793)

Utilizator Mihai145Oprea Mihai Adrian Mihai145 Data 3 martie 2019 17:58:05
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int NMAX = 100005;

int N, M, nrc;
vector <int> g[NMAX];
bool vis[NMAX];

void DFS(int node)
{
    vis[node] = 1;

    for(auto it : g[node])
        if(vis[it] == 0)
            DFS(it);
}

int main()
{
    int x, y;

    fin >> N >> M;

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

    for(int i = 1; i <= N; i++)
        if(vis[i] == 0)
        {
            DFS(i);
            ++nrc;
        }

    fout << nrc << '\n';

    return 0;
}