Pagini recente » Cod sursa (job #3203775) | Cod sursa (job #2974842) | Cod sursa (job #2709412) | Cod sursa (job #3162000) | Cod sursa (job #2448325)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n,m,s,x,y,paths[100001];
vector<int>lista[100001];
bool use[100001];
set<pair<int,int>>coada;
int main()
{
fin>>n>>m>>s;
for(int i=1;i<=m;i++)
{
fin>>x>>y;
lista[x].push_back(y);
}
coada.insert(make_pair(0,s));
use[s]=1;
while(!coada.empty())
{
auto it=coada.begin();
int nod=(*it).second;
coada.erase(it);
for(int i=0;i<lista[nod].size();i++)
if(lista[nod][i]!=nod&&use[lista[nod][i]]==0)
{
paths[lista[nod][i]]=1+paths[nod];
coada.insert(make_pair(paths[lista[nod][i]],lista[nod][i]));
use[lista[nod][i]]=1;
}
}
for(int i=1;i<=n;i++)
{
if(paths[i]==0&&i!=s)
fout<<-1;
else
fout<<paths[i];
fout<<' ';
}
return 0;
}