Pagini recente » Cod sursa (job #1428063) | Cod sursa (job #119939) | Cod sursa (job #685627) | Cod sursa (job #2613314) | Cod sursa (job #489134)
Cod sursa(job #489134)
#include<fstream>
#include<list>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
# define nmax 100002
list <int> a[nmax];
int cost[nmax],n,m,s,coada[nmax],nod[nmax];
void read()
{
f>>n>>m>>s;int i,j;
while(f>>i>>j) a[i].push_back(j);
}
void bfs()
{
int i=1,j=1,x; memset(cost,-1,sizeof(cost));
coada[i]=s;cost[s]++;
while(i<=j)
{
while(a[coada[i]].size())
{
x=a[coada[i]].front();a[coada[i]].pop_front();
if(cost[x]==-1){j++; coada[j]=x;}
if(cost[coada[i]]<cost[x]||cost[x]==-1) cost[x]=cost[coada[i]]+1;
}
i++;
}
}
int main()
{
read();bfs();int i;
for(i=1;i<=n;i++) g<<cost[i]<<" ";
}