Pagini recente » Cod sursa (job #2729413) | Cod sursa (job #2729422) | Cod sursa (job #3336708) | Cod sursa (job #3334223) | Cod sursa (job #3322748)
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <fstream>
#include <bitset>
#include <queue>
#include <map>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector<int> graph[100001];
int minDist[100001];
void bfs(int startNode) {
queue<int> q;
q.push(startNode);
minDist[startNode] = 0;
while (!q.empty()) {
int currentNode = q.front();
q.pop();
for (vector<int>::iterator it = graph[currentNode].begin(); it != graph[currentNode].end(); ++it) {
if (minDist[*it] == -1) {
minDist[*it] = minDist[currentNode] + 1;
q.push(*it);
}
}
}
}
int main() {
int n, m, s;
fin >> n >> m >> s;
for (int i = 1; i <= m; ++i) {
int x, y;
fin >> x >> y;
graph[x].push_back(y);
}
for (int i = 1; i <= n; ++i) {
minDist[i] = -1;
}
bfs(s);
for (int i = 1; i <= n; ++i) {
fout << minDist[i] << ' ';
}
return 0;
}
/*
0 0 0 5 6
7 7 1 1 1
1 1 1 3 1
1 1 2 2 1
0 0 9 0 0
0 0 0 0 9
*/