Pagini recente » Cod sursa (job #2433279) | Cod sursa (job #3243664) | Cod sursa (job #2204017) | Cod sursa (job #2827024) | Cod sursa (job #2823319)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, s, fr[100005], d[100005];
vector<int>h[100005];
int Bfs(int x)
{
queue<int>q;
q.push(x);
fr[x] = 1;
while(!q.empty())
{
x = q.front();
q.pop();
for(auto i : h[x])
if(fr[i] == 0)
{
q.push(i);
d[i] = d[x] + 1;
fr[i] = 1;
}
}
}
int main()
{
int x, y;
fin >> n >> m >> s;
for(int i = 1; i <= m; i++)
{
fin >> x >> y;
h[x].push_back(y);
///h[y].push_back(x);
}
Bfs(s);
for(int i = 1; i <= n; i++)
if(i != s && d[i] == 0)
fout << "-1 ";
else fout << d[i] << " ";
return 0;
}