Pagini recente » Cod sursa (job #1258763) | Cod sursa (job #1396960) | Cod sursa (job #1338874) | Cod sursa (job #1671885) | Cod sursa (job #1453714)
#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
#define nmax 100005
using namespace std;
void get_data(int &n, int &s, vector<int> v[nmax]){
ifstream fin ("bfs.in");
int m, x, y;
fin >> n >> m >> s;
for(int i= 0; i< m; i++){
fin >> x >> y;
v[x].push_back(y);
}
}
void bfs(int &n, int &s, vector<int> v[nmax], int dist[nmax]){
queue<int> q;
q.push(s);
dist[s]= 0;
while(!q.empty()){
int x= q.front();
q.pop();
for(auto y: v[x])
if(dist[y]< 0){
dist[y]= dist[x]+ 1;
q.push(y);
}
}
}
int main(){
ios_base::sync_with_stdio(false);
ofstream fout("bfs.out");
int dist[nmax], n, s;
vector<int> v[nmax];
for(auto &x: dist) x= -1;
get_data(n, s, v);
bfs(n, s, v, dist);
for(int i=1; i<=n; i++) fout << dist[i] << " ";
return 0;
}