Pagini recente » Cod sursa (job #1921267) | Cod sursa (job #2740107) | Cod sursa (job #602074) | Cod sursa (job #2680847) | Cod sursa (job #2130227)
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
const int N=100001;
vector<int> a[N];
int m,n,q[N],d[N],st,dr=1,s;
void citire(){
int x,y;
in>>n>>m>>s;
for(int i=0;i<m;i++){
in>>x>>y;
a[x].push_back(y);
}
}
int main()
{
int x,y;
citire();
for (int i = 1; i <= n; i++)
{
d[i] = -1;
}
q[++dr]=s;
d[s]=0;
while(st<=dr){
x=q[st++];
for(int i=0;i<a[x].size(); i++)
{
y=a[x][i];
if(d[y]==-1){
q[++dr]=y;
d[y]=1+d[x];
}
}
}
for(int i=1;i<=n;i++)
out<<(i==s ? 0 : d[i])<<" ";
return 0;
}