Pagini recente » Cod sursa (job #1162520) | Cod sursa (job #604104) | Cod sursa (job #15674) | Cod sursa (job #3245957) | Cod sursa (job #2678184)
#include <bits/stdc++.h>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
queue < pair < int,int > > q;
vector <int> l[100005];
int n,m,s,sol[100005];
void citire()
{
in>>n>>m>>s;
for(; m--;)
{
int a,b;
in>>a>>b;
l[a].push_back(b);
}
}
void bfs()
{
while(!q.empty())
{
int x=q.front().first;
int y=q.front().second;
q.pop();
for( vector <int> :: iterator i=l[x].begin(); i!=l[x].end(); i++)
{
if(sol[*i]==-1)
{
sol[*i]=y+1;
q.push({*i,y+1});
}
}
}
}
void prg()
{
fill(sol+1,sol+n+1,-1);
q.push({s,0});
bfs();
}
void afis()
{
sol[s]=0;
for(int i=1; i<=n; i++)
out<<sol[i]<<' ';
}
int main()
{
citire();
prg();
afis();
in.close();
out.close();
return 0;
}