Pagini recente » Cod sursa (job #3249674) | Cod sursa (job #1309773) | Cod sursa (job #847086) | Cod sursa (job #730901) | Cod sursa (job #920625)
Cod sursa(job #920625)
#include <fstream>
using namespace std;
ifstream in ("dfs.in");
ofstream out ("dfs.out");
struct nod {int inf; nod *next;} *L[100001];
int n,m,used[100001],st[100001],k;
void ins(nod *&first , int inf)
{
nod *p = new nod;
p->inf = inf;
p->next = first;
first = p;
}
/*void l()
{
for(int i=1;i<=k;i++)
{
nod *p=new nod;
p=L[st[i]];
while(p!=0)
{
int current_nod = p->inf;
if(used[current_nod]==0)
{
st[++k]=current_nod;
used[current_nod]++;
}
p=p->next;
}
}
}*/
void l(int x)
{
used[x]=1;
nod * p =L[x];
while(p!=0)
{
int c=p->inf;
if(used[c]==0)
l(c);
p=p->next;
}
}
int main()
{
in>>n>>m;
for(int i(1);i<=m;i++)
{
int x,y;
in>>x>>y;
ins(L[x],y);
ins(L[y],x);
}
int Y=0;
for(int i(1);i<=n;i++)
if(used[i]==0){Y++; l(i);}
out<<Y;
return 0;
}