Pagini recente » Cod sursa (job #1965641) | Cod sursa (job #2604228) | Cod sursa (job #1876410) | Cod sursa (job #1200115) | Cod sursa (job #1913173)
#include <bits/stdc++.h>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
const int nMax = 100003;
const int inf = 1e9;
int dp[nMax], n, m ,s;
queue <int> Q;
vector <int> Graf[nMax];
inline void Bfs(int start) {
for(int i = 1; i <= n; i++) {
dp[i] = inf;
}
dp[start] = 0;
Q.push(start);
while(!Q.empty()) {
int nod = Q.front();
Q.pop();
for(const auto &i : Graf[nod]) {
if(dp[i] > dp[nod] + 1) {
dp[i] = dp[nod] + 1;
Q.push(i);
}
}
}
}
int main()
{
int x, y;
f >> n >> m >> s;
for(int i = 1; i <= m; i++){
f >> x >> y;
Graf[x].push_back(y);
}
Bfs(s);
for(int i = 1; i <= n; i++) {
if(dp[i] == inf) {
g << "-1 ";
continue;
}
g << dp[i] << " ";
}
return 0;
}