Cod sursa(job #2031356)

Utilizator R.DavidDavid Rusu R.David Data 3 octombrie 2017 07:03:42
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1 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;
        }
		else
		{
			good  = true;
		}

        if(good)
        {
            nd[node] = -1;
            legaturi ++;
            dfs(nds[node][i]);
        }
    }
}