Cod sursa(job #662183)

Utilizator Byby8Ene Bianca Byby8 Data 16 ianuarie 2012 01:41:22
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
#include <fstream>
using namespace std;
#define max 100002
ifstream f("dfs.in");
ofstream g("dfs.out");

struct nod{long info; nod *urm;};
char v[max];
long n,comp_con,m,i;
nod *l[max];

void adaugare(nod *&p, long x)
{nod *c;
c=new nod;
c->info=x;
c->urm=0;
if (p!=0) 
p=c;
else
{c->urm=p;
p=c;
}
}

void df(int i)
{nod *c;
v[i]=1;
c=l[i];
while (c)
{df(c->info);
c=c->urm;
}
}

int main()
{long x,y;
f>>n>>m;
for (i=1;i<=m;i++)
{f>>x>>y;
adaugare(l[x],y);
adaugare(l[y],x);
}
for (i=1;i<=n;i++)
{if (v[i]==0) 
{comp_con++;
 df(i);}
g<<comp_con;}
f.close();
g.close();
return 0;

}