Pagini recente » Cod sursa (job #888715) | Cod sursa (job #699160) | Cod sursa (job #748884) | Cod sursa (job #1750081) | Cod sursa (job #1116173)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#define nmax 100005
using namespace std;
vector <int> vecin[nmax];
queue <int> q;
int cost[nmax];
int n, m, s, x, y, urmator;
void bfs() {
while(!q.empty()) {
int curent = q.front();
q.pop();
for(int i=0; i<vecin[curent].size(); i++) {
urmator = vecin[curent][i];
if(cost[urmator] == -1) {
cost[urmator] = cost[curent] + 1;
q.push(urmator);
}
}
}
}
int main() {
ifstream f("bfs.in");
ofstream g("bfs.out");
f>>n>>m>>s;
for(int i=1; i<=n; i++) cost[i] =-1;
cost[s] = 0;
for(int i=1; i<=m; i++) {
f>>x>>y;
vecin[x].push_back(y);
}
q.push(s);
bfs();
for(int i=1; i<=n; i++)
g<<cost[i]<<" ";
return 0;
}