Pagini recente » Profil Djok | Istoria paginii runda/rf_3 | Istoria paginii runda/gametheory | Profil CorinaT | Cod sursa (job #233640)
Cod sursa(job #233640)
#include <fstream>
using namespace std;
struct nod
{
int x;
nod *urm;
};
nod *vf[100001];
int n,m,s;
int vect[3][100001];
bool viz[100001];
void adauga(int ex1,int ex2){
nod *p=new nod;
p->urm=vf[ex1];
p->x=ex2;
vf[ex1]=p;
}
void bfs(int y){
for (int i=1;i<=n;i++)
vect[1][i]=-1;
nod *p;
int st,sp,pasi=0;
st=1;sp=1;
vect[1][y]=0;
vect[2][st]=y;
viz[y]=true;
while (st<=sp){
y=vect[2][st];
for (p=vf[y];p!=NULL;p=p->urm){
if (viz[p->x]==false){
sp++;
viz[p->x]=true;
vect[1][p->x]=vect[1][vect[2][st]]+1;
vect[2][sp]=p->x;
}
}
st++;
}
}
void afisare(){
fstream fout("bfs.out",ios::out);
for (int i=1;i<=n;i++)
fout<<vect[1][i]<<" ";
}
void citire(){
fstream fin("bfs.in",ios::in);
int ex1,ex2;
fin>>n>>m>>s;
for (int i=1;i<=m;i++){
fin>>ex1>>ex2;
adauga(ex1,ex2);
}
}
int main(){
citire();
bfs(s);
afisare();
}