Pagini recente » Cod sursa (job #1779386) | Cod sursa (job #2521451) | Cod sursa (job #525142) | Cod sursa (job #1704605) | Cod sursa (job #2772532)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int n, m, x;
int dist[100001];
queue<int>q;
vector<int>adiacenta[100001];
void bfs(int x)
{
q.push(x);
dist[x] = 1;
while (!q.empty())
{
int curent = q.front();
q.pop();
for (auto x : adiacenta[curent])
{
if (!dist[x])
{
dist[x] = dist[curent]+1;
q.push(x);
}
}
}
}
int main()
{
f >> n >> m >> x;
for (int i = 1; i <= m; i++)
{
int nr1, nr2;
f >> nr1 >> nr2;
adiacenta[nr1].push_back(nr2);
}
bfs(x);
for (int i = 1; i <= n; i++)
{
g << dist[i] - 1 << " ";
}
}