Pagini recente » Cod sursa (job #500447) | Cod sursa (job #1332169) | Cod sursa (job #1456166) | Cod sursa (job #1754670) | Cod sursa (job #1782013)
#include <fstream>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n,m,viz[100002];
struct nod
{
int v;
nod *urm;
};
nod *L[100002], *p;
void fill(int x)
{
viz[x]=1;
for(nod *p=L[x];p!=0;p=p->urm)
{
int y=p->v;
if(viz[y]==0)
{
fill(y);
}
}
}
int i,x,y,c;
int main()
{
fin>>n>>m;
for(i=1;i<=m;i++)
{
fin>>x>>y;
p=new nod;
p->v=y;
p->urm=L[x];
L[x]=p;
p=new nod;
p->v=x;
p->urm=L[y];
L[y]=p;
}
c=0;
for(i=1;i<=n;i++)
{
if(viz[i]==0)
{
c++;
fill(i);
}
}
fout<<c;
return 0;
}