Cod sursa(job #2863678)

Utilizator ButunoiButunoi Daniel Adrian Butunoi Data 7 martie 2022 08:34:43
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("dfs.in"); ofstream g("dfs.out");
vector <int> A[100002]; ///A[x]=un vector din STL care contine lista de adiacenta a lui x
bool viz[100002];
///LISTE DE ADIACENTA REPREZENTATE CA VECTORI DIN STL
int n, m, nr;
void DFS(int x)
{   viz[x]=1;
    vector <int> :: iterator it=A[x].begin(),sf=A[x].end();
    for ( ; it!=sf; it++)
        if(!viz[*it]) DFS(*it);
}
int main()
{   f>>n>>m;
    for (int x,y,i=0; i<m; i++)
    {   f>>x>>y;
        A[x].push_back(y);///adaug pe y in lista de adiacenta a lui x
        A[y].push_back(x);///adaug pe x in lista de adiacenta a lui y
    }
    for (int i=1; i<=n; i++)
        if (!viz[i]) {nr++; DFS(i);}
    g<<nr<<'\n';
    g.close(); f.close(); return 0;
}