Pagini recente » Atasamentele paginii Oxyzer[TM] | Borderou de evaluare (job #2710819) | Istoria paginii problema/kinder | Cod sursa (job #767301)
Cod sursa(job #767301)
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define MAX 100001
#define INF 0xfffffff
vector<int>g[MAX];
int d[MAX],n,s,c[MAX];
void bfs(){
int x,y,p,u;
for(int i=1;i<=n;i++) d[i] = INF;
d[s] = 0;
c[p = u = 1] = s;
while( p <= u )
{
x = c[p++];
for(int i=0;i<g[x].size();i++)
{
y = g[x][i];
if( d[y] > d[x] + 1 )
{
d[y] = d[x] + 1;
c[++u] = y;
}
}
}
}
int main(){
int m,x,y;
freopen("bfs.in","r",stdin);
freopen("bfs.out","w",stdout);
scanf("%d %d %d",&n,&m,&s);
while(m--)
{
scanf("%d %d",&x,&y);
g[x].push_back(y);
}
bfs();
for(int i=1;i<=n;i++) printf("%d ",d[i] == INF && i != s ? -1 : d[i]);
return 0;
}