Cod sursa(job #2548333)
| Utilizator | Data | 16 februarie 2020 15:35:51 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.67 kb |
#define NMAX 100005
#include <fstream>
#include <vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n, m, viz[NMAX];
vector<int> graph[NMAX];
void read()
{
int x, y;
f>>n>>m;
for(int i = 0; i < m; ++i)
{
f>>x>>y;
graph[x].push_back(y);
graph[y].push_back(x);
}
}
void dfs(int nod)
{
viz[nod] = 1;
for(auto &v:graph[nod])
if(viz[v] == 0)
dfs(v);
}
int main()
{
read();
int nr = 0;
for(int i = 1; i <= n; ++ i)
if(viz[i] == 0)
{
dfs(i);
nr++;
}
g<<nr;
return 0;
}
