Pagini recente » Cod sursa (job #2638045) | Cod sursa (job #1776336) | Produse | Cod sursa (job #1700157) | Cod sursa (job #2270580)
#include <bits/stdc++.h>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
const int N = 1e5+5;
const int INF = 1e6+5;
vector<int> v[N];
int dist[N];
int main()
{
int n,m,s;
in >> n >> m >> s;
for (int i = 1; i<=m; i++)
{
int x,y;
in >> x >> y;
v[x].push_back(y);
}
queue<int> q;
q.push(s);
dist[s] = 1;
while (!q.empty())
{
int now = q.front();
for (auto it: v[now])
if (!dist[it])
{
dist[it] = 1+dist[now];
q.push(it);
}
q.pop();
}
for (int i = 1; i<=n; i++)
out << dist[i]-1 << " ";
}