Pagini recente » Cod sursa (job #2262519) | Cod sursa (job #1386628) | Borderou de evaluare (job #1569012) | Cod sursa (job #2138719) | Cod sursa (job #349942)
Cod sursa(job #349942)
#include<fstream>
#define Max 20000
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n,m,s,a[Max][Max],b[Max],c[Max],pus[Max];
void read(){
int x,y;
fin>>n>>m>>s;
for(int i = 1; i <= m; i++)
{
fin>>x>>y;
a[x][0]++;
a[x][a[x][0]] = y;
}
fin.close();
}
int gasit(int lin,int nod){
for(int i = 1; i <= a[lin][0]; i++)
if(a[lin][i] == nod)
return 1;
return 0;
}
void bf(){
int st,dr,i;
st = 1;
dr = 1;
while(st <= dr){
for(i = 1; i <= n;i++)
if(!pus[i] && gasit(b[st],i) == 1)
{
pus[i] = 1;
dr++;
b[dr] = i;
c[i] = c[b[st]]+1;
}
st++;
}
}
int main(){
read();
pus[s] = 1;
b[1] = s;
bf();
for(int i = 1; i <= n; i++)
if(pus[i] == 0)
c[i] = -1;
for(int i = 1; i <= n; i++)
fout<<c[i]<<" ";
fout.close();
return 0;
}