Pagini recente » Cod sursa (job #494404) | Cod sursa (job #1205781) | Cod sursa (job #2807088) | Cod sursa (job #2626100) | Cod sursa (job #2494321)
#include <iostream>
#include <fstream>
#include <list>
#include <iterator>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
list<int>graph[200005];
queue<int>nodeToBeVisited;
int marked[200005];
int n, m, x, y, source;
void BFS(int currentNode){
marked[currentNode] = 0;
nodeToBeVisited.push(currentNode);
while(!nodeToBeVisited.empty()){
currentNode = nodeToBeVisited.front();
for(list<int>::iterator it = graph[currentNode].begin(); it != graph[currentNode].end(); it++)
if(marked[*it] == -1){
marked[*it] = marked[currentNode] + 1;
nodeToBeVisited.push(*it);
}
nodeToBeVisited.pop();
}
}
int main()
{
fin >> n >> m >> source;
for(int i = 1; i <= m; i++){
fin >> x >> y;
graph[x].push_back(y);
}
for(int i = 1; i <= m; i++)
marked[i] = -1;
BFS(source);
for(int i = 1; i <= n; i++)
fout << marked[i] << ' ';
return 0;
}