Cod sursa(job #2629333)

Utilizator Ionut_neuer58Raducu Ioan Stefan Ionut_neuer58 Data 20 iunie 2020 10:58:53
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.55 kb
#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

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

vector <int> v[100001];
bool viz[100001];
int n, m, c, xx, yy;

void readit()
{
    in>>n>>m;
    while(m--) in>>yy>>xx, v[yy].push_back(xx), v[xx].push_back(yy);
}

void DFS(int nod)
{
    viz[nod]=1;
    for(int i=0; i<v[nod].size(); i++)
        if(!viz[v[nod][i]]) DFS(v[nod][i]);
}

int main()
{
    readit();
    for(int i=1; i<=n; i++)
        if(!viz[i]) c++, DFS(i);
    out<<c;
    return 0;
}