Pagini recente » Rating Martin Ioana Evelina (Evelina2002) | Cod sursa (job #121635) | Cod sursa (job #1899134) | Cod sursa (job #2882259) | Cod sursa (job #2462290)
#include <iostream>
#include <fstream>
using namespace std;
ifstream x("bfs.in");
ofstream y("bfs.out");
int n,m,s,i,j,k,viz[100002],c[100002],pi,ps,cost[100002],t[2][1000002],start[100002],p,nr;
int main()
{
x>>n>>m>>s;
while(x>>i>>j)
{
if(i!=j)
{
k++;
t[0][k]=j;
t[1][k]=start[i];
start[i]=k;
}
}
pi=ps=1;
c[1]=s;
viz[s]=1;
while(ps<=pi)
{
p=start[c[ps]];
nr=start[c[ps]];
while(p)
{
if(viz[t[0][p]]==0)
{
pi++;
c[pi]=t[0][p];
viz[t[0][p]]=1;
if(cost[nr]+1<=cost[t[0][p]] || cost[t[0][p]]==0)
cost[t[0][p]]=cost[nr]+1;
}
p=t[1][p];
}
ps++;
}
for(i=1;i<=n;i++)
{
if(i!=s && cost[i]==0)
y<<-1<<" ";
else
y<<cost[i]<<" ";
}
x.close();
y.close();
return 0;
}