Cod sursa(job #2241177)

Utilizator cristinaandrei10Andrei Cristina cristinaandrei10 Data 15 septembrie 2018 10:53:19
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
const int N = 100010;
vector<int> v[N];
int n,m,c,x,y,viz[N];
void dfs(int);
int main()
{
    f >> n >> m;
    for ( ; m; m--)
    {
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for( int i = 1 ; i <= n ; i++)
        if(!viz[i])
        {
            c++;
            dfs(i);
        }
    g<<c<<'\n';
    return 0;
}
void dfs(int nod)
{
    viz[nod]=1;
    for(auto vec:v[nod])
        if(!viz[vec])
            dfs(vec);
}