Pagini recente » Statistici Liviu Ioan (liviu_ioan) | Rating Nadolu Robert-Alexandru (NADOLU) | Profil Georgi | Rating faaa a (red8red) | Cod sursa (job #2863676)
#include <bits/stdc++.h>
using namespace std;
ifstream f ("dfs.in"); ofstream g ("dfs.out");
vector <int> L[100001];
bool viz[100001];
int n,m,nr;
void dfs(int s)
{ viz[s]=true;
for(int i = 0;i < L[s].size();++i)
if(viz[L[s][i]] == false) dfs(L[s][i]);
}
int main()
{ f>>n>>m;
for(int x,y,i = 0;i < m;++i)
{ f >> x >> y;
L[x].push_back(y); //Edge from vertex x to vertex y
L[y].push_back(x); //Edge from vertex y to vertex x
}
for(int i = 1;i <= n;++i)
if(viz[i] == false) { dfs(i); nr++; }
g<<nr<<'\n';
g.close();
f.close();
return 0;
}