Pagini recente » Cod sursa (job #469659) | Cod sursa (job #1646538)
#include <fstream>
using namespace std;
ifstream cin("dfs.in");
ofstream cout("dfs.out");
bool viz[100005];
int N, M, x, y, cnt;
typedef struct nod{
int info;
nod* next;
} *lnod;
lnod A[100005];
void add(lnod &a, int x){
lnod b = new nod;
b->info = x;
b->next = a;
a = b;
}
void dfs(int nodul){
viz[nodul] = 1;
for(lnod l = A[nodul]; l; l = l->next ) if(!viz[l -> info]) dfs(l->info);
}
int main(){
cin >> N >> M;
for(int i = 1; i <= M; i++){
cin >> x >> y;
add(A[x],y);
add(A[y],x);
}
for(int i = 1; i <= N; i++){
if(!viz[i]){ dfs(i);
cnt++;}
}
cout << cnt;
return 0;
}