Pagini recente » Cod sursa (job #2758788) | Cod sursa (job #1269986) | Cod sursa (job #2870882) | Cod sursa (job #2378792) | Cod sursa (job #2953825)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
const int NMAX = 1e5 + 7;
int main()
{
int n, m, s;
fin >> n >> m >> s;
vector<vector<int>> v(NMAX);
vector<bool> ok (NMAX, false);
vector<int> dist(NMAX, -1);
for(int i = 1; i <= m; i++)
{
int x, y;
fin >> x >> y;
v[x].push_back(y);
}
queue<int> q;
q.push(s);
ok[s] = true;
dist[s] = 0;
while(!q.empty())
{
int x = q.front();
q.pop();
for(auto it : v[x])
{
if(!ok[it])
{
ok[it] = true;
dist[it] = dist[x] + 1;
q.push(it);
}
}
}
for(int i = 1; i <= n; i++)
{
fout << dist[i] << " ";
}
return 0;
}