Cod sursa(job #2372778)
| Utilizator | Data | 7 martie 2019 11:04:15 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 55 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.64 kb |
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
vector <int> graph [10001];
int v[10001];
void DFS(int n)
{
v[n]=1;
int lim=graph[n].size();
for(int i=0;i<lim;i++)
{
if(v[graph[n][i]]==0)
DFS(graph[n][i]);
}
}
int main()
{
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int N,M,x,y,nr=0;
fin>>N>>M;
for(int i=1;i<=M;i++)
{
fin>>x>>y;
graph[x].push_back(y);
graph[y].push_back(x);
}
for(int i=1;i<=N;i++)
{
if(v[i]==0) nr++;
DFS(i);
}
fout<<nr;
return 0;
}
