Pagini recente » Cod sursa (job #545166) | Cod sursa (job #1347310) | Cod sursa (job #2870130) | Cod sursa (job #3295559) | Cod sursa (job #3298948)
#include <iostream>
#include <bits/stdc++.h>
#define KAL 3214432
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector<int>netanyahu[KAL];
queue<int>lapid;
map<int,int>v;
int main()
{
int n,m,s;
fin>>n>>m>>s;
int a,b;
for(int i =1;i<=n;i++){
v[i]=-1;
}
for(int i = 0;i<m;i++){
fin>>a>>b;
netanyahu[a].push_back(b);
}
lapid.push(s);
v[s]=0;
int q= 1;
while(!lapid.empty()){
int bezalel=lapid.front();
for(int i =0;i<netanyahu[bezalel].size();i++){
if(v[netanyahu[bezalel][i]]==-1){
v[netanyahu[bezalel][i]]=v[bezalel]+1;
lapid.push(netanyahu[bezalel][i]);
}
}
lapid.pop();
}
std::map<int,int>::iterator it=v.begin();
while(it!=v.end()){
fout<<it->second<<" ";
it++;
}
return 0;
}