Pagini recente » Rating Silas Codruta (balanta) | Cod sursa (job #2096740) | Cod sursa (job #1871224) | Cod sursa (job #440767) | Cod sursa (job #768671)
Cod sursa(job #768671)
# include <fstream>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int N,M,viz[100005],c;
typedef struct nod
{
int x;
nod *y;
} *qnod;
qnod v[100005];
void adaugare(qnod &dest, int val)
{
qnod q;
q = new nod;
q->x=val;
q->y=dest;
dest=q;
}
void citire()
{
f>>N>>M;
int i,a,b;
q= new nod;
for (i=1;i<=M;i++)
{
f>>a>>b;
adaugare(v[x],y);
adaugare(v[y],x);
}
f.close();
}
void DFS(int nod)
{
qnod q;
viz[nod]=1;
for (q=v[nod]; q!=NULL; q=q->y)
if(viz[p->x]==0)
DFS(p->x);
}
int main()
{
citire();
int i;
for (i=1;i<=N;i++)
if(viz[i]==0)
{
c++;
DFS(i);
}
g<<c;
g.close();
return 0;
}