Cod sursa(job #2031269)

Utilizator R.DavidDavid Rusu R.David Data 2 octombrie 2017 22:23:09
Problema Parcurgere DFS - componente conexe Scor 5
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 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++)
    {
        dfs(i);
    }

    out<<legaturi;
}

void dfs(int node)
{
    bool good = true;

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