Cod sursa(job #2699719)
| Utilizator | Data | 25 ianuarie 2021 18:09:39 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.69 kb |
#include <fstream>
#include <vector>
using namespace std;
const int N=100001;
bool viz[N];
int n,m,nc;
vector <int> a[N];
ifstream in("dfs.in");
ofstream out("dfs.out");
void dfs(int x)
{
viz[x] = true;
for(auto y:a[x])
{
if(!viz[y])
{
dfs(y);
}
}
}
int main()
{
in >> n >> m;
for(int i=1; i <= m; i++)
{
int x,y;
in >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
for(int i=1; i <= n; i++)
{
if(!viz[i])
{
nc++;
dfs(i);
}
}
out << nc;
in.close();
out.close();
return 0;
}
