Pagini recente » Cod sursa (job #2835083) | Cod sursa (job #473873) | Cod sursa (job #341254) | Cod sursa (job #745198) | Cod sursa (job #2867651)
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
const int N = 100000;
int d[N+5];
queue <int> q;
vector <int> a[N+5];
int main()
{
int n, m, s, x, y;
fin >> n >> m >> s;
for(int i = 1; i <= m; i++)
{
fin >> x >> y;
a[x].push_back(y);
}
for(int i = 1; i <= n; i++)
{
d[i] = -1;
}
q.push(s);
d[s] = 0;
while(!q.empty())
{
int x = q.front();
q.pop();
for(auto y: a[x])
{
if(d[y] == -1)
{
d[y] = d[x] + 1;
q.push(y);
}
}
}
for(int i = 1; i <= n; i++)
{
fout << d[i] << " ";
}
return 0;
}