Pagini recente » Cod sursa (job #909930) | Cod sursa (job #2149630) | Cod sursa (job #2895161) | Cod sursa (job #2979535) | Cod sursa (job #2567930)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
const int maxn = 100005;
vector <int> v[maxn];
int dist[maxn];
queue <int> q;
int main()
{
int n, m, s;
in >> n >> m >> s;
for(int i = 1; i <= m; i++)
{
int x, y;
in >> x >> y;
v[x].push_back(y);
}
for(int i = 1; i <= n; i++)
dist[i] = (1 << 30);
dist[s] = 0;
q.push(s);
while(!q.empty())
{
int nod = q.front();
q.pop();
for(auto it : v[nod])
{
if(dist[it] > dist[nod] + 1)
{
dist[it] = dist[nod] + 1;
q.push(it);
}
}
}
for(int i = 1; i <= n; i++)
{
if(dist[i] >= (1 << 30))
out << -1 << " ";
else
out << dist[i] << " ";
}
out << "\n";
return 0;
}