Pagini recente » Cod sursa (job #433704) | Cod sursa (job #2885723) | Borderou de evaluare (job #171878) | Cod sursa (job #2602062) | Cod sursa (job #2118789)
#include <bits/stdc++.h>
using namespace std;
int N,M,dp[100010];
vector<int> V[100010];
bool viz[100010];
void bfs(int x){
for (int i = 1; i <= N;i++) dp[i] = - 1;
dp[x] = 0;
queue<int> Q;
Q.push(x);
while (!Q.empty()){
int nod = Q.front();
Q.pop();
viz[nod] = 1;
for (auto it : V[nod]){
if (!viz[it]){
dp[it] = dp[nod] + 1;
Q.push(it);
}
}
}
}
int main(){
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int x;
fin >>N>>M>>x;
for(int a,b;M--;){
fin >> a >> b;
V[a].push_back(b);
}
bfs(x);
for (int i = 1; i <= N;i++) fout <<dp[i]<<" ";
return 0;
}