Pagini recente » Cod sursa (job #3241082) | Cod sursa (job #1978775) | Utilizatori inregistrati la .com 2009, Runda 2 | Cod sursa (job #2909635) | Cod sursa (job #920595)
Cod sursa(job #920595)
#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;
}
}
}
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);
}
if(m==0)
{
out<<n;
return 0;
}
k=1;
st[k]=1;
l();
int Y=1;
for(int i(1);i<=n;i++)
{
if(L[i]!=0){
if(used[i]==0)
{
Y++;
k=1;
st[k]=i;
l();
}
}
else Y++;
}
out<<Y;
return 0;
}