Pagini recente » Cod sursa (job #240026) | Cod sursa (job #2621034) | Cod sursa (job #2880533) | Cod sursa (job #2875174) | Cod sursa (job #3300119)
#include <bits/stdc++.h>
using namespace std;
int const N=100005;
int noduri, arce, dist[N], target;
vector<int>arc[N];
queue<int>q;
void bfs(int nod)
{
dist[nod]=0;
q.push(nod);
while(!q.empty())
{
int nod_curent=q.front();
q.pop();
for(auto i:arc[nod_curent])
if(dist[i]==N)
dist[i]=dist[nod_curent]+1, q.push(i);
}
}
int main()
{
ifstream cin("bfs.in");
ofstream cout("bfs.out");
for(int i=1; i<=100000; i++)
dist[i]=N;
cin>>noduri>>arce>>target;
int x, y;
for(int i=1; i<=arce; i++)
{
cin>>x>>y;
arc[x].push_back(y);
}
bfs(target);
for(int i=1; i<=noduri; i++)
if(dist[i]==N)
dist[i]=-1;
for(int i=1; i<=noduri; i++)
cout<<dist[i]<<" ";
return 0;
}