Pagini recente » Statistici Iliescu Alexia (iliescualexia) | You will lose | Cod sursa (job #2937564) | Cod sursa (job #2862279) | Cod sursa (job #2029005)
#include <bits/stdc++.h>
#define N 100005
using namespace std;
int n, m, s, d[N];
bool o[N];
vector<int> G[N];
queue<int> Q;
void BFS(int s)
{
Q.push(s);
o[s] = 1;
while (!Q.empty())
{
s = Q.front();
Q.pop();
for (auto x: G[s])
if (!o[x])
{
o[x] = 1;
d[x] = d[s] + 1;
Q.push(x);
}
}
}
int main()
{
freopen("bfs.in", "r", stdin);
freopen("bfs.out", "w", stdout);
scanf("%i%i%i", &n, &m, &s);
int x, y;
while (m--)
{
scanf("%i%i", &x, &y);
G[x].push_back(y);
}
BFS(s);
for (int i = 1; i <= n; i++)
if (!o[i]) printf("-1 ");
else printf("%i ", d[i]);
return 0;
}