Cod sursa(job #2665962)

Utilizator CoakazeRotaru Catalin Coakaze Data 31 octombrie 2020 16:02:36
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int n, m, comp_conexe;
vector<int> gr[100005];
int viz[100005];

int dfs(int node)
{
    if(viz[node])
        return 0;
    viz[node] = 1;
    for(int v : gr[node])
        dfs(v);
}

int main()
{
    ifstream f("dfs.in");
    ofstream g("dfs.out");
    f>>n>>m;
    int x, y;
    for(int i = 0; i < m; i++)
    {
        f>>x>>y;
        gr[x].push_back(y);
        gr[y].push_back(x);
    }
    for(int i = 1; i <= n; i++)
    {
        if(!viz[i])
        {
            dfs(i);
            comp_conexe ++;
        }
    }
    g<<comp_conexe;
    return 0;
}