Pagini recente » Cod sursa (job #3323177) | Cod sursa (job #1454357) | Cod sursa (job #3321834) | Statistici Gutanu Andreea Meda Georgiana (Gutanu_Meda) | Cod sursa (job #3317920)
#include <iostream>
#include <unordered_map>
#include <vector>
#include <queue>
#include <fstream>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int main(){
int n,m,s;
fin>>n>>m>>s;
unordered_map<int, vector<int>> adj;
vector<int> dist(n+1, -1);
for(int i=0;i<m;i++){
int x,y;
fin>>x>>y;
adj[x].push_back(y);
}
queue<int> q;
q.push(s);
dist[s] = 0;
while(!q.empty()){
int node = q.front();
q.pop();
for(int neighbor: adj[node]){
if(dist[neighbor] == -1){
dist[neighbor] = dist[node] + 1;
q.push(neighbor);
}
}
}
for(int i = 1; i <= n; i++){
fout<<dist[i]<<" ";
}
return 0;
}