Cod sursa(job #2927593)
| Utilizator | Data | 20 octombrie 2022 21:51:01 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.65 kb |
#include<iostream>
#include<fstream>
#include<stack>
#include<vector>
#define MAX 100005
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n,m,comp_con=0;
int vizitat[MAX];
vector<int> lista[MAX];
void dfs(int i)
{
vizitat[i]=1;
for(auto j:lista[i])
{
if(vizitat[j]==0) dfs(j);
}
}
int main()
{
int i,j,x,y;
f>>n>>m;
for(i=1;i<=m;i++)
{
f>>x>>y;
lista[x].push_back(y);
lista[y].push_back(x);
}
for(int i=1;i<=n;i++)
{if(vizitat[i]==0)
comp_con++;
dfs(i);}
g<<comp_con;
}
