Pagini recente » Cod sursa (job #865322) | Cod sursa (job #3004042) | Cod sursa (job #490152) | Cod sursa (job #285082) | Cod sursa (job #2270659)
#include <fstream>
#include <list>
#include <queue>
using namespace std;
ifstream f ("bfs.in");
ofstream g ("bfs.out");
list <int> graf[100001];
queue <int> q;
int n;
int dist[100001];
void BFS(int k)
{
dist[k]=1;
q.push(k);
list <int> :: iterator it;
while(!q.empty()){
for(it=graf[q.front()].begin(); it!=graf[q.front()].end(); it++)
if(!dist[*it]){
q.push(*it);
dist[*it]=dist[q.front()]+1;
}
q.pop();
}
}
int main()
{
int m, start;
f >> n >> m >> start;
for(int i = 1, x, y; i <= m; i++){
f >> x >> y;
graf[x].push_back(y);
}
BFS(start);
for(int i = 1; i <= n; i++)
g << dist[i] - 1 << " ";
return 0;
}