Pagini recente » Cod sursa (job #1883967) | Cod sursa (job #2507914) | Cod sursa (job #768025) | Cod sursa (job #3208059) | Cod sursa (job #2682699)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
queue <int> q;
int n, m, source;
vector <int> G[100001];
int path[100001];
int main()
{
int x, y;
fin>>n>>m>>source;
for(int i=0; i<m; i++)
{
fin>>x>>y;
G[x].push_back(y);
}
for(int i=1; i<=n; i++)
path[i] = -1;
q.push(source);
path[source] = 0;
while(!q.empty())
{
int node = q.front();
q.pop();
for(auto it:G[node])
if(path[it] == -1)
{
path[it] = path[node] + 1;
q.push(it);
}
}
for(int i=1; i<=n; i++)
fout<<path[i]<<' ';
return 0;
}