Cod sursa(job #1988069)
Utilizator | Vasiu Alexandru alex.vasiu | Data | 2 iunie 2017 00:01:45 |
---|---|---|---|
Problema | Parcurgere DFS - componente conexe | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.66 kb |
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
vector<int> Graph[100005];
vector<bool> is;
int n, m;
void dfs(int nod)
{
is[nod] = true;
for (const auto& val : Graph[nod])
if (!is[val])
dfs(val);
}
int main()
{
int x, y;
f >> n >> m;
is = vector<bool> (n + 1);
while (m--)
{
f >> x >> y;
Graph[x].push_back(y);
Graph[y].push_back(x);
}
int nr = 0;
for (int i = 1; i <= n; i++)
if (!is[i])
{
dfs(i);
nr++;
}
g << nr;
}