Mai intai trebuie sa te autentifici.
Cod sursa(job #3209345)
| Utilizator | Data | 2 martie 2024 11:40:12 | |
|---|---|---|---|
| Problema | BFS - Parcurgere in latime | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.83 kb |
#include <fstream>
#include <queue>
#include <iostream>
#include <vector>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
const int NMAX = 100001;
int n, m,s;
int dis[NMAX];
vector<int> v[NMAX];
void citire(){
in >> n >> m>> s;
for(int i = 0 ; i < m ; i ++){
int x,y;
in >> x>> y;
if(x!=y)
v[x].push_back(y);
}
}
void bfs(){
queue<int> q;
dis[s] = 1;
q.push(s);
while(!q.empty()){
int nod = q.front();
q.pop();
for ( int i= 0; i < v[nod].size(); i++){
int neigh = v[nod][i];
if(!dis[neigh]){
q.push(neigh);
dis[neigh]= dis[nod]+1;
}
}
}
}
int main(){
citire();
bfs();
for (int i = 1 ; i <= n; i ++){
cout <<dis[i]-1 << " ";
}
}