Pagini recente » Cod sursa (job #1578263) | Cod sursa (job #2037702) | Cod sursa (job #2420468) | Cod sursa (job #453285) | Cod sursa (job #1583941)
#include <cstdio>
#include <vector>
using namespace std;
vector <int> g[100001];
int q[100001], sol[100001];
bool sel[100001];
void bfs (int x)
{
int i, p=1, r=1, y, nod; q[1]=x; sel[x]=true; sol[x]=0;
while (p<=r)
{
nod=q[p];
for (i=0; i<g[nod].size(); i++)
{
y=g[nod][i];
if (!sel[y])
{
q[++r]=y;
sol[y]=sol[nod]+1;
sel[y]=true;
}
}
p++;
}
}
int main()
{
int n, m, S, i, x, y;
freopen("bfs.in","r",stdin);
freopen("bfs.out","w",stdout);
scanf("%d%d%d",&n,&m,&S);
for (i=1; i<=n; i++) sol[i]=-1;
for (i=1; i<=m; i++)
{
scanf("%d%d",&x,&y);
g[x].push_back(y);
}
bfs(S);
for (i=1; i<=n; i++) printf("%d ",sol[i]);
return 0;
}