Pagini recente » Cod sursa (job #1908291) | Cod sursa (job #424660) | Cod sursa (job #471493) | Cod sursa (job #3188632) | Cod sursa (job #2620357)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector<int> lista[100010];
int freq[100010][2];
void bfs(int start) {
queue<pair<int, int> > q;
q.push(make_pair(start, 0));
freq[start][0] = 1;
freq[start][1] = 0;
while (!q.empty()) {
pair<int, int> top = q.front();
q.pop();
for (int i = 0; i < lista[top.first].size(); i++)
if (!freq[lista[top.first][i]][0]) {
q.push(make_pair(lista[top.first][i], top.second + 1));
freq[lista[top.first][i]][0] = 1;
freq[lista[top.first][i]][1] = top.second + 1;
}
}
}
int main() {
int n, m, s;
int x, y;
fin >> n >> m >> s;
for (int i = 1; i <= m; i++) {
fin >> x >> y;
lista[x].push_back(y);
}
bfs(s);
for (int i = 1; i <= n; i++)
if (freq[i][0] == 0)
fout << -1 << " ";
else
fout << freq[i][1] << " ";
return 0;
}