Pagini recente » Diferente pentru problema/lianyu intre reviziile 2 si 1 | Profil GRIND_to_ONI | Cod sursa (job #1152036) | Cod sursa (job #2791290)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
int n, m, S, v[100001];
vector<int> g[100001];
queue<int> q;
void read()
{
in >> n >> m >> S;
int x, y;
for(int i = 0; i < m; ++i)
in >> x >> y, g[x].push_back(y);
}
void bfs(int s)
{
q.push(s), v[s] = 1;
while(!q.empty())
{
for(auto i : g[q.front()])
if(!v[i]) q.push(i), v[i] = v[q.front()] + 1;
q.pop();
}
}
void afis()
{
for(int i = 1; i <= n; ++i)
out << (v[i] ? v[i]-1 : -1) << ' ';
}
int main()
{
read();
bfs(S);
afis();
return 0;
}