Cod sursa(job #2255577)

Utilizator Cristi01052Tudorache Christian Cristi01052 Data 7 octombrie 2018 11:53:55
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int LIM = 100005;
int n, m, viz[LIM];
vector <int> gr[LIM];

void dfs (int nod)
{
    viz[nod] = 1;
    for(int i = 0; i < gr[nod].size(); ++i)
    {
        if(viz[ gr[nod][i] ] == 0)
        {
            //viz[ gr[nod][i] ] = 1;
            dfs (gr[nod][i]);
        }
    }
}


int main()
{
    cin >> n >> m;
    for(int i = 1; i <= m; ++i)
    {
        int x, y;
        cin >> x >> y;
        gr[x].push_back(y);
        gr[y].push_back(x);
    }
    int k=0;
    for(int i = 1; i <= n; ++i)
    {
        if(viz[i] == 0)
        {
            dfs (i);
            k++;
        }
    }
    cout<<k;
    return 0;
}