Pagini recente » Cod sursa (job #2241927) | Cod sursa (job #2131562) | Monitorul de evaluare | Cod sursa (job #2198888) | Cod sursa (job #2791290)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
int n, m, S, v[100001];
vector<int> g[100001];
queue<int> q;
void read()
{
in >> n >> m >> S;
int x, y;
for(int i = 0; i < m; ++i)
in >> x >> y, g[x].push_back(y);
}
void bfs(int s)
{
q.push(s), v[s] = 1;
while(!q.empty())
{
for(auto i : g[q.front()])
if(!v[i]) q.push(i), v[i] = v[q.front()] + 1;
q.pop();
}
}
void afis()
{
for(int i = 1; i <= n; ++i)
out << (v[i] ? v[i]-1 : -1) << ' ';
}
int main()
{
read();
bfs(S);
afis();
return 0;
}