Pagini recente » Cod sursa (job #2339184) | Cod sursa (job #2374267) | Cod sursa (job #3175479) | Istoria paginii runda/oji2017/clasament | Cod sursa (job #2312882)
#include <iostream>
#include <fstream>
#include <list>
using namespace std;
void dfs(list<int> *L, int x[], int i){
x[i] = 1;
for(list<int>::iterator it = L[i].begin(); it != L[i].end(); it++){
if(x[*it] == 0){
dfs(L, x, *it);
}
}
}
int main()
{
ifstream f("dfsinput.in");
ofstream o("dfsoutput.out");
int a,b;
f>>b>>a;
int c,d;
int k = 0;
list<int> *L = new list<int>[b+1];
while (a--){
f>>c>>d;
L[c].push_back(d);
L[d].push_back(c);
}
f.close();
int x[b+1] = {0};
for(int i = 1; i < b+1; i++){
if(x[i] == 0){
dfs(L, x, i);
k++;
}
}
o<<k;
o.close();
return 0;
}