Pagini recente » Cod sursa (job #423804) | Cod sursa (job #2655611) | Cod sursa (job #333902) | Cod sursa (job #98187) | Cod sursa (job #2262067)
#include <fstream>
#include <list>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
bool conex[100005];
int main()
{
int n, m, i, ct = 0, x, y;
list<int> s, l[100005];
fin >> n >> m;
for(i = 1; i <= m; i++){
fin >> x >> y;l[x].push_back(y);
l[y].push_back(x);
}
for(i = 1; i <= n; i++){
if(conex[i]) continue;
s.push_back(i); ct++;
while(!s.empty()){
x = s.back(); s.pop_back();
while(!l[x].empty()){
y = l[x].front();l[x].pop_front();
conex[y] = 1; s.push_back(y);
}
}
}
fout << ct;
return 0;
}