Pagini recente » Cod sursa (job #242636) | Cod sursa (job #186289) | Cod sursa (job #445219)
Cod sursa(job #445219)
#include<cstdio>
#include<vector>
#define N 1000001
using namespace std;
int n,m,s;
int d[N],pred[N],coada[N];
void citire();
void bfs();
vector<int>vec[N];
int main()
{
freopen("bfs.in","r",stdin);
freopen("bfs.out","w",stdout);
citire();
bfs();
for(int i=1;i<=n;++i)
printf("%d ",d[i]);
return 0;
}
void citire()
{
int x,y;
scanf("%d%d%d ", &n,&m,&s);
while(m--)
{
scanf("%d%d",&x,&y);
vec[x].push_back(y);
}
}
void bfs()
{
int p=0,u=0,i,x,y;
d[s]=0;
for(i=1;i<=n;++i)
d[i]=-1;
p=u=0;
coada[u++]=s;
d[s]=0;
while(p!=u)
{
x=coada[p++];
for(size_t i=0;i<vec[x].size();++i)
{
y=vec[x][i];
if(d[y]==-1)
{
d[y]=d[x]+1;
coada[u++]=y;
pred[y]=x;
}
}
}
}