Pagini recente » Cod sursa (job #2961549) | Cod sursa (job #1950084) | Cod sursa (job #2240018) | Cod sursa (job #1492147) | Cod sursa (job #2732157)
#include <bits/stdc++.h>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
const int INF=1e9;
int n,m,s,x,y;
vector<int> v[100005];
int dist[100005];
queue<int> q;
void bfs()
{
q.push(s);
dist[s]=0;
while(!q.empty())
{
int nod=q.front();
q.pop();
for (auto it:v[nod])
{
if (dist[it]>dist[nod]+1)
{
dist[it]=dist[nod]+1;
q.push(it);
}
}
}
}
int main()
{
f>>n>>m>>s;
for (int i=1; i<=m; i++)
{
f>>x>>y;
v[x].push_back(y);
}
for (int i=1;i<=n;i++)
dist[i]=INF;
bfs();
for (int i=1;i<=n;i++)
if (dist[i]==INF)
g<<-1<<" ";
else
g<<dist[i]<<" ";
return 0;
}