Pagini recente » Cod sursa (job #2651168) | Cod sursa (job #385387) | Cod sursa (job #1770218) | Cod sursa (job #11428) | Cod sursa (job #2461755)
#include <bits/stdc++.h>
#define maxi 100003
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
vector <int> vec[maxi];
queue <int> coada;
int cost[maxi];
void bfs(int s)
{
coada.push(s);
cost[s] = 0;
while (!coada.empty())
{
int nod = coada.front();
coada.pop();
for (auto next : vec[nod])
if (cost[next] == 0 && next != s)
{
cost[next] = cost[nod] + 1;
coada.push(next);
}
}
}
int main()
{
int n, m, s;
f >> n >> m >> s;
while (m --)
{
int x, y;
f >> x >>y;
vec[x].push_back(y);
}
bfs(s);
for (int nod = 1; nod <= n; ++ nod)
if (cost[nod] == 0 && nod != s)
g << -1 << " ";
else
g << cost[nod] << " ";
}