Pagini recente » Cod sursa (job #2506954) | Cod sursa (job #1705371) | Statistici Homescu Monica (homescu.monica) | Cod sursa (job #1705126) | Cod sursa (job #2312887)
#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 g("dfsoutput.out");
int m,n;
f>>n>>m;
int p,h;
int k=0;
list<int> *Liste= new list<int>[n+1];
while(m--){
f>>p>>h;
Liste[p].push_back(h);
Liste[h].push_back(p);
}
f.close();
int x[n+1]={0};
for(int i=1;i<n+1;i++) {
if(x[i]==0) {
dfs(L, x, i);
k++;
}
}
g<<k;
g.close();
return 0;
}