Pagini recente » Cod sursa (job #1867097) | Cod sursa (job #2343472) | Cod sursa (job #773565) | Cod sursa (job #620400) | Cod sursa (job #650508)
Cod sursa(job #650508)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
vector<int>vecini[100];
int lista[100];
int viz[100];
int nod,m,n;
void citire(){
int a,b;
f>>n>>m>>nod;
for(int i=1;i<=m;i++){
f>>a>>b;
vecini[a].push_back(b);
}
f.close();
}
void bf(int x){
memset(viz, -1, sizeof(viz));
lista[1]=x;
viz[x]=0;
int el_act=1,ult_el=1,nr;
while(el_act<=ult_el){
nod=lista[el_act];
for(int i=0;i<vecini[nod].size();i++){
nr=vecini[nod][i];
if(viz[nr]==-1) lista[++ult_el]=nr,viz[lista[ult_el]]=viz[lista[el_act]]+1;
}
el_act++;
}
}
int main(){
citire();
bf(nod);
for(int i=1;i<=n;i++)
g<<viz[i]<<' ';
g<<endl;
g.close();
return 0;
}