Pagini recente » Cod sursa (job #2594409) | Cod sursa (job #1709777) | Cod sursa (job #190276) | Cod sursa (job #1933116) | Cod sursa (job #632096)
Cod sursa(job #632096)
#include <cstdio>
#define nMax 100100
using namespace std;
int n;
int m;
int viz[nMax];
int s;
struct nod{
int x;
nod *urm;
}*a[nMax];
void citire()
{
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int x;
int y;
scanf("%d%d",&x,&y);
nod *z;
z = new nod;
z -> x = y;
z -> urm = a[x];
a[x] = z;
nod *Z;
Z = new nod;
Z -> x = x;
Z -> urm = a[y];
a[y] = Z;
}
}
void fill(int k)
{
for(nod *i=a[k] ; i ; i=i->urm)
{
if( !viz[i -> x] )
{
viz[i -> x] = 1;
fill( i -> x );
}
}
}
void rez()
{
for(int i=1;i<=n;i++)
{
if(!viz[i])
{
viz[ i ] = 1;
s++;
fill(i);
}
}
printf("%d\n", s);
}
int main()
{
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
citire();
rez();
return 0;
}