Pagini recente » Istoria paginii runda/omg_am_revenit | Diferente pentru home intre reviziile 902 si 112 | Cod sursa (job #2079891) | Monitorul de evaluare | Cod sursa (job #2197730)
#include <bits/stdc++.h>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int N, M, S, x, y;
vector<int> a[100003];
queue<pair<int, int>> q;
bool visit[100003];
int bfs(int x) {
int pas = 0;
while(!q.empty()) q.pop();
for(auto i = a[S].begin(); i != a[S].end(); i++)
q.push({*i, 1});
if(S == x)
return pas;
while(!q.empty()) {
if(q.front().first == x)
return q.front().second;
if(!visit[q.front().first]) {
for(auto i = a[q.front().first].begin(); i != a[q.front().first].end(); i++)
q.push({*i, q.front().second + 1});
}
visit[q.front().first] = true;
q.pop();
}
return -1;
}
int main()
{
f >> N >> M >> S;
for(int i = 0; i < M; i++)
f >> x >> y, a[x].push_back(y);
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= N; j++)
visit[j] = false;
g << bfs(i) << " ";
}
g << "\n";
return 0;
}