Pagini recente » Cod sursa (job #2656109) | Cod sursa (job #3161961) | Cod sursa (job #3142654) | Cod sursa (job #1380105) | Cod sursa (job #2547163)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("bfs.in");
ofstream fout ("bfs.out");
const int nMax = 100010;
vector <int> ans;
vector <bool> prez;
vector <int> v[nMax];
void bfs(int source)
{
queue <int> q;
q.push(source);
ans[source] = 0;
while(!q.empty())
{
int nod = q.front(); q.pop();
if(prez[nod]) continue;
prez[nod] = true;
for(auto it : v[nod])
{
if(ans[it] != -1) continue;
ans[it] = ans[nod] + 1;
q.push(it);
}
}
}
int main()
{
int n, m, s; fin >> n >> m >> s; s--;
ans.assign(n, -1);
prez.assign(n, 0);
for(int i=0; i<m; i++)
{
int x, y; fin >> x >> y; x--; y--;
v[x].push_back(y);
}
bfs(s);
for(auto it : ans)
fout << it << " ";
return 0;
}