Pagini recente » Cod sursa (job #2814786) | Cod sursa (job #1803773) | Cod sursa (job #948371) | Cod sursa (job #2946187) | Cod sursa (job #2537458)
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100001;
vector <int> nod[MAX];
deque <int> coada;
int viz[MAX], n, m, s, lung[MAX];
ifstream in ("bfs.in");
ofstream out ("bfs.out");
int BFS()
{
int x;
coada.push_back(s);
viz[s] = 1;
lung[s] = 0;
while(!coada.empty())
{
x = coada.front();
coada.pop_front();
for(int i = 0; i < nod[x].size(); i++)
{
if(!viz[nod[x][i]])
{
coada.push_back(nod[x][i]);
lung[nod[x][i]] = lung[x] + 1;
viz[nod[x][i]] = 1;
}
}
}
}
int main()
{
int x, y;
in >> n >> m >> s;
while(m)
{
m--;
in >> x >> y;
nod[x].push_back(y);
}
for(int i = 1; i <= n; i++)
{
lung[i] = -1;
}
BFS();
for(int i = 1; i <= n; i++)
{
out << lung[i] << " ";
}
return 0;
}