Pagini recente » Cod sursa (job #175427) | Cod sursa (job #2018878) | Cod sursa (job #2960661) | Cod sursa (job #2489075) | Cod sursa (job #2692130)
#include <bits/stdc++.h>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
vector <int> l[100005];
int n, m, s, p, q;
int v[100005];
void bfs(int nod)
{
v[nod] = 1;
p = q = 0;
queue <int> c;
c.push(nod);
while(!c.empty())
{
int t = c.front();
for(int i = 0; i < l[t].size(); i++)
if(v[l[t][i]] == 0)
{
c.push(l[t][i]);
v[l[t][i]] = v[t] + 1;
}
c.pop();
}
}
int main()
{
f >> n >> m >> s;
for(int i = 1; i <= m; i++)
{
int x, y;
f >> x >> y;
l[x].push_back(y);
}
bfs(s);
for(int i = 1; i <= n; i++)
if(v[i] == 0)
g << -1 << " ";
else
g << v[i] - 1 << " ";
return 0;
}