Pagini recente » Cod sursa (job #3130157) | Cod sursa (job #680479)
Cod sursa(job #680479)
#include<fstream>
#include<list>
#include<stdio.h>
using namespace std;
#define IN "dfs.in"
#define OUT "dfs.out"
#define MaxN 200050
fstream f(IN, ios::in), g(OUT, ios::out);
list<long>a[MaxN];
list<long>::iterator it;
int x, y, n, m, i, cnt=0;
bool viz[MaxN];
void BFS(int start)
{
int p, u, coada[MaxN];
p=u=1;
coada[1]=start;
viz[start]=true;
while(p<=u)
{
for(it=a[start].begin(); it!=a[start].end(); it++)
{
if(viz[*it]==false)
{
viz[*it]=true;
u++;
coada[u]=*it;
}
}
p++;
start=coada[p];
}
}
int main()
{
f>>n>>m;
for(i=1; i<=n; i++)
{
f>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
for(i=1; i<=n; i++)
{
if(viz[i]==false)
{
cnt++;
BFS(i);
}
}
g<<cnt<<endl;
return 0;
}