Pagini recente » Cod sursa (job #1056649) | Cod sursa (job #3273917) | Cod sursa (job #3290593) | Cod sursa (job #3290411) | Cod sursa (job #3286825)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int N, M, S, nrcc=0;
vector <int> l[100005];
queue <int> q;
int viz[100005];
void bfs(int nod)
{
int aux;
q.push(nod);
viz[nod]=1;
while(!q.empty())
{
aux=q.front();
for(auto &p : l[aux])
{
if(viz[p]==0)
{
viz[p]=viz[aux]+1;
q.push(p);
}
}
q.pop();
}
}
int main()
{
int x, y;
fin >> N >> M;
for(int i=1; i<=M; i++)
{
fin >> x >> y;
l[x].push_back(y);
}
for(int i=1;i<=N;i++)
{
if(viz[i]==0)
{
bfs(i);
nrcc++;
}
}
fout << nrcc;
}