Pagini recente » Cod sursa (job #2906554) | Cod sursa (job #2947031) | Cod sursa (job #2298699) | Cod sursa (job #1243121) | Cod sursa (job #1289131)
#include <iostream>
#include <fstream>
using namespace std;
const int N=100001,M=1000001;
ifstream fin("bfs.in");
ofstream out("bfs.out");
int vf[M],urm[M],lst[N],coada[M],d[N];
bool ver[N];
int nr,n,m,k;
int st=1;
int dr=0;
void adauga(int x, int y){
nr++;
vf[nr]=y;
urm[nr]=lst[x];
lst[x]=nr;
}
void bfs(int x)
{
int p,y;
coada[++dr]=x;
for(int i=1;i<=n;i++) d[i]=-1;
d[x]=0;
while (st<=dr)
{
x=coada[st++];
p=lst[x];
while(p!=0){
y=vf[p];
if(d[y]==-1){
d[y]=1+d[x];
coada[++dr]=y;
}
p=urm[p];
}
}
}
void citre(){
fin>>n>>m>>k;
int a,b;
for(int i=1;i<=m;i++){
fin>>a>>b;
adauga(a,b);
}
}
int main()
{
citre();
bfs(k);
for(int i=1;i<=dr;i++) cout<<coada[i];
//for(int i=1;i<=n;i++)
//if(sol[i]==0) sol[i]=-1;
//sol[k]=0;
for(int i=1;i<=n;i++)
out<<d[i]<<" ";
return 0;
}