Cod sursa(job #2031616)

Utilizator R.DavidDavid Rusu R.David Data 3 octombrie 2017 16:50:31
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;

void dfs(int);

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

int n,m,legaturi = 0;

vector<int> nds[100005],nd;

int main()
{
    int i ,x ,y;

    input>>n>>m;

    for(i = 1; i <= m; i++)
    {
        input>>x>>y;
        nds[x].push_back(y);
        nds[y].push_back(x);
    }

    for(i = 0; i <= n; i++)
    {
       nd.push_back(0);
    }

    for(i = 1; i <= n; i++)
    {
       if(nd[i] != -1)
       {
            legaturi ++;
            dfs(i);
       }
    }

    out<<legaturi;
}

void dfs(int node)
{
    bool good;
    nd[node] = -1;

    for(int i = 0; i < nds[node].size(); i++)
    {
        if(nd[nds[node][i]] != -1)
        {
            dfs(nds[node][i]);
        }
    }
}