Pagini recente » Cod sursa (job #2219102) | Cod sursa (job #1760636) | Cod sursa (job #427788) | Cod sursa (job #1487805) | Cod sursa (job #3239203)
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
const int nmax = 100005;
int n,m,s;
vector<int> a[nmax],cost(100005,-1);
queue<int> q;
void bfs(int nod){
q.push(s);
cost[s] = 0;
while(!q.empty()){
for(int vec: a[q.front()])
if(cost[vec] == -1){
cost[vec] = cost[q.front()]+1;
q.push(vec);
}
q.pop();
}
}
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int main(){
fin>>n>>m>>s;
while(m--){
int x,y;
fin>>x>>y;
a[x].push_back(y);
}
bfs(s);
for(int i = 1; i <= n; i++)fout<<cost[i]<<" ";
}