Pagini recente » Cod sursa (job #3266172) | Cod sursa (job #2650174) | Cod sursa (job #1127510) | Cod sursa (job #108122) | Cod sursa (job #2658641)
#include <bits/stdc++.h>
#define nmax 100007
using namespace std;
ifstream fin ("bfs.in");
ofstream fout ("bfs.out");
int n, m, el;
vector<int> L[nmax];
int viz[nmax];
int drum[nmax];
queue<int> q;
void Citire()
{
int x, y;
fin >> n >> m >> el;
for (int i = 1; i <= m; i++)
{
fin >> x >> y;
L[x].push_back(y);
}
}
void BFS(int nod)
{
drum[nod] = 0;
q.push(nod);
viz[nod] = 1;
while(!q.empty())
{
nod = q.front();
q.pop();
for (auto w : L[nod])
if (viz[w] == 0)
{
drum[w] = drum[nod] + 1;
viz[w] = 1;
q.push(w);
}
}
}
int main()
{
Citire();
BFS(el);
for (int i = 1; i <= n; i++)
{
if (i != el && drum[i] == 0)
drum[i] = -1;
fout << drum[i] << " ";
}
fout << "\n";
fin.close();
fout.close();
return 0;
}