Pagini recente » Cod sursa (job #554459) | Cod sursa (job #2834984) | Cod sursa (job #1709114) | Cod sursa (job #3241768) | Cod sursa (job #1417317)
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
vector <int> v[200001];
int i,j,n,x,y,m,s,cost[200001],coada[200001],gg[200001];
void bfs(int nod)
{
int l=1;
cost[nod]=0;
coada[l]=nod;
for(i=1;i<=l;i++)
for(j=0;j<=gg[coada[i]];j++)
if(cost[v[coada[i]][j]]==-1)
{
l++; coada[l]=v[coada[i]][j];
cost[coada[l]]=cost[coada[i]]+1;
}
}
int main()
{
f>>n>>m>>s; //de la s la i
while(m>0)
{
f>>x>>y;
v[x].push_back(y);
m--;
}
for(i=1;i<=n;i++) {gg[i]=v[i].size();cost[i]=-1;}
bfs(s);
for(i=1;i<=n;i++) g<<cost[i]<<" ";
f.close();
g.close();
return 0;
}