Cod sursa(job #2846791)

Utilizator DMR6476Erdic Dragos DMR6476 Data 9 februarie 2022 17:50:19
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
vector<vector<int> > theNeighbours;
bitset <100005> isThere;
void dfs(int node)
{
    isThere[node] = 1;
    vector<int> :: iterator eachOne;
    for(eachOne = theNeighbours[node].begin(); eachOne != theNeighbours[node].end(); eachOne++)
    {
        int currNode = (*eachOne);
        if(isThere[currNode] == false)
        {
            dfs(currNode);
        }
    }
}
int main()
{
    int n,m;
    fin>>n>>m;
    theNeighbours = vector<vector<int> > (n + 1);
    for(int i = 1; i <= m ; i++)
    {
        int x,y;
        fin>>x>>y;
        theNeighbours[x].push_back(y);
        theNeighbours[y].push_back(x);
    }
    int cc = 0;
    for(int i = 1; i <= n; i++)
    {
        if(isThere[i] == 0)
        {
            ++cc;
            dfs(i);
        }
    }
    fout<<cc;
    return 0;
}