Pagini recente » Cod sursa (job #1252666) | Cod sursa (job #1471528) | Cod sursa (job #392691) | Cod sursa (job #3151520) | Cod sursa (job #2681549)
#include <bits/stdc++.h>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
const int NMAX = 1e5;
int N, M;
vector<int> G[NMAX + 2];
queue <int > q;
int check[100005];
int s;
int nod;
void bfs(int start)
{
q.push(start);
check[start] = 1;
while(!q.empty())
{
nod = q.front();
q.pop();
for(int i = 0; i < G[nod].size(); ++i)
{
if(check[G[nod][i]] == 0)
{
check[G[nod][i]] = check[nod] + 1;
q.push(G[nod][i]);
}
}
}
}
int main()
{
in >> N >> M >> s;
for (int i = 1; i <= M; ++i) {
int x,y;
in >> x >> y;
G[x].push_back(y);
}
bfs(s);
for (int i = 1; i <= N; ++i) {
out << check[i] - 1 << " ";
}
}