Pagini recente » Cod sursa (job #1782440) | Cod sursa (job #1304408) | Cod sursa (job #1245155) | Cod sursa (job #1042968) | Cod sursa (job #1719139)
#include <bits/stdc++.h>
#define valmax 999999
using namespace std;
ifstream fin("bfs.in");ofstream fout("bfs.out");
int main()
{
int n,m,x,y,origine;
fin>>n>>m>>origine;
list <int> L[n+1];
for(int i=0;i<m;++i){fin>>x>>y;L[x].push_back(y);}
vector <int> cost(n+1,valmax);
cost[origine]=0;
queue <int> Q;
Q.push(origine);
while(Q.size())
{
int nod=Q.front();
Q.pop();
for(auto& it:L[nod])
{
if(cost[it]>cost[nod]+1)
{
cost[it]=cost[nod]+1;
Q.push(it);
}
}
}
for(int i=1;i<=n;++i)
{
if(cost[i]==valmax)fout<<-1<<' ';
else fout<<cost[i]<<' ';
}
return 0;
}