Mai intai trebuie sa te autentifici.
Cod sursa(job #2387465)
| Utilizator | Data | 24 martie 2019 18:24:01 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.72 kb |
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("date.in");
vector <int> graph [1000];
int viz[100];
void dfs (int node)
{
viz[node] = 1;
int dim = graph[node].size(); //nr vecini nod
for(int i=0; i<dim; i++)
{
int vecin = graph[node][i];
if(!viz[vecin])
dfs(vecin);
}
}
int main ()
{
int n, m, x, y;
f>>n>>m;
for(int i=0; i<m; i++)
{
f>>x>>y;//muchiile
graph[x].push_back(y);
graph[y].push_back(x);
}
int nr = 0;
for(int i=1; i<=n; i++)
if(!viz[i])
{
dfs(i);
nr++;
}
cout<<"nr comp conexe="<<nr;
}
