Cod sursa(job #2538318)

Utilizator Katherine456719Swan Katherine Katherine456719 Data 4 februarie 2020 17:37:22
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
vector<int>v[100001];
int n,m,viz[1000001],p;
void citire()
{
    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);
    }
}
void dfs(int nod)
{
    viz[nod] = true;
    int curent;
    for(int i = 0; i < v[nod].size(); i++)
    {
        curent = v[nod][i];
        if(viz[curent]==0)
            dfs(curent);
    }
}
int main()
{
    citire();
    for(int i=1;i<=n;i++)
    { if(!viz[i])
       {
           p++;
        dfs(i);
       }
    }
    fout<<p;
    return 0;
}