Cod sursa(job #2678077)

Utilizator razvanradulescuRadulescu Razvan razvanradulescu Data 28 noiembrie 2020 09:20:46
Problema Parcurgere DFS - componente conexe Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream f("dfs.in");
ofstream g("dfs.out");

struct nod
{
    int val;
    vector<int> vecin;
} graf[100005];
int n, m;
bool viz[100005];

void citire()
{
    f>>n>>m;
    int x, y;
    for(int i = 1; i<=n; i++)
    {
        f>>x>>y;
        graf[x].vecin.push_back(y);
        graf[y].vecin.push_back(x);
    }
}

void dfs(int currNode)
{
    viz[currNode] = true;
    for(auto &i : graf[currNode].vecin)
    {
        if(!viz[i])
        {
            dfs(i);
        }
    }
}

void rez()
{
    int nrComp = 0;
    for(int i = 1; i<=n; i++)
    {
        if(!viz[i])
        {
            nrComp++;
            dfs(i);
        }
    }
    g<<nrComp;
}

int main()
{
    citire();
    rez();
    return 0;
}