Pagini recente » Cod sursa (job #1819860) | Calibrare limite de timp | Cod sursa (job #2107352) | Istoria paginii runda/simularea-care-a-spart-globul-pamantesc | Cod sursa (job #2838699)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, s;
const int N = 1e5 + 1;
vector <int> G[N];
bool viz[N];
int f[N];
void bfs(int x)
{
queue <int> Q;
for (int i = 1; i <= n; i++)
f[i] = -1;
viz[x] = 1;
f[x] = 0;
Q.push(x);
while (!Q.empty())
{
int v = Q.front();
for (auto it : G[v])
{
if (!viz[it])
{
viz[it] = 1;
f[it] = f[v] + 1;
Q.push(it);
}
}
Q.pop();
}
}
int main()
{
fin >> n >> m >> s;
for (int i = 1; i <= m; i++)
{
int x, y;
fin >> x >> y;
G[x].push_back(y);
}
bfs(s);
for (int i = 1; i <= n; i++)
fout << f[i] << " ";
return 0;
}