Cod sursa(job #662018)
#include <iostream>
#include <fstream>
#define MAXIM 100005
using namespace std;
struct nod
{int inf;
nod *urm;}*a[MAXIM];
int N, M, viz[MAXIM];
void adaugare (int i, int t)
{
nod *q;
q=new nod;
q->inf=t;
q->urm=a[i];
a[i]=q;
}
void DFS(int k)
{
nod *q;
viz[k]=1;
while (q)
{if (viz[q->inf]==0)
DFS(q->inf);
q=q->urm;}
}
int main()
{
int i, j, X, Y, componente=0;
ifstream f("dfs.in");
ofstream g("dfs.out");
f>>N>>M;
for (i=0; i<M; i++)
{f>>X>>Y;
adaugare(X,Y);
adaugare(Y,X);}
for (j=0; j<N; j++)
if (viz[i]==0)
{DFS(i);
componente++;}
g<<componente;
return 0;
}