Mai intai trebuie sa te autentifici.
Cod sursa(job #1856987)
Utilizator | Data | 25 ianuarie 2017 18:04:32 | |
---|---|---|---|
Problema | Parcurgere DFS - componente conexe | Scor | 5 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.52 kb |
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
const int N=100000;
int n,m;
int x,y;
int nr;
vector <int> a[N];
bool viz[N];
void citire()
{
fin>>n>>m;
for (int i=0;i<m;i++) {
fin>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
}
void dfs(int x)
{
for (int i=0;i<a[x].size();i++) {
y=a[x][i];
viz[y]=true;
}
}
int main()
{
citire();
for (int i=1;i<=n;i++) if (!viz[i]) {dfs(i);nr++;}
fout<<nr;
}