Pagini recente » Cod sursa (job #1815508) | Cod sursa (job #1759246) | Cod sursa (job #405098) | Cod sursa (job #622658) | Cod sursa (job #420567)
Cod sursa(job #420567)
#include <cstdio>
#include <vector>
using namespace std;
#define MAX 100005
FILE* fout=fopen("bfs.out","w");
vector<int>nod[MAX];
int dist[MAX],list[MAX],n,m,s,ls=0;
//<parsing>
FILE* fin=fopen("bfs.in","r");
const unsigned maxb=8192;
char buf[maxb];
unsigned ptr=0;
inline unsigned getInt(){
unsigned nr=0;
while(buf[ptr]<'0'||'9'<buf[ptr])
if(++ptr>=maxb)
fread(buf,maxb,1,fin),ptr=0;
while('0'<=buf[ptr]&&buf[ptr]<='9'){
nr=nr*10+buf[ptr]-'0';
if(++ptr>=maxb)
fread(buf,maxb,1,fin),ptr=0;
}
return nr;
}
//</parsing>
void bfs(int beg){
list[0]=beg,ls=0;
dist[beg]=0;
for(int j=0;j<=ls;j++){
beg=list[j];
for(int i=0;i<nod[beg].size();i++){
if(dist[nod[beg][i]]==-1){
list[++ls]=nod[beg][i];
dist[nod[beg][i]]=dist[beg]+1;
}
}
}
}
int main(){
int x,y;
n=getInt(),m=getInt(),s=getInt();
for(int i=0;i<m;i++){
x=getInt(),y=getInt();
nod[x-1].push_back(y-1);
}
for(int i=0;i<n;i++)
dist[i]=-1;
bfs(s-1);
for(int i=0;i<n;i++)
fprintf(fout,"%d ",dist[i]);
fclose(fin);
fclose(fout);
return 0;
}