Pagini recente » Istoria paginii problema/cern | Borderou de evaluare (job #2804410) | Cod sursa (job #2429690)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector <int> v[100001];
queue <int> Q;
int viz[100001], n, m, s, i, x, y;
void bfs()
{
Q.push(s);
viz[s] = 0;
while(!Q.empty())
{
int x = Q.front();
Q.pop();
for(int i = 0; i < v[x].size(); ++ i)
if(viz[v[x][i]] == -1)
{
Q.push(v[x][i]);
viz[v[x][i]] = viz[x] + 1;
}
}
}
int main()
{
fin >> n >> m >> s;
while(m --)
{
fin >> x >> y;
v[x].push_back(y);
}
for(i = 1; i <= n; ++ i) viz[i] = -1;
bfs();
for(i = 1; i <= n; ++ i)
fout << viz[i] << " ";
return 0;
}