Pagini recente » Cod sursa (job #837816) | Cod sursa (job #3238671) | Cod sursa (job #938583) | Cod sursa (job #2768594) | Cod sursa (job #3286820)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int N, M, S;
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 >> S;
for(int i=1; i<=M; i++)
{
fin >> x >> y;
l[x].push_back(y);
}
bfs(S);
for(int i=1;i<=N;i++)
{
fout << viz[i]-1 << " ";
}
}