Pagini recente » Cod sursa (job #139109) | Cod sursa (job #3234202) | Cod sursa (job #888877) | Cod sursa (job #126304) | Cod sursa (job #2124699)
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
#define N 100003
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n,m,s;
int x,y;
int dist[N];
vector<int> g[N];
queue<int> q;
bitset<N> f;
int main()
{
fin>>n>>m>>s;
while(m--)
{
fin>>x>>y;
g[x].push_back(y);
}
q.push(s);
f[s] = 1;
for(int i=1; i<=n; ++i)
dist[i] = -1;
dist[s] = 0;
while(!q.empty())
{
int nod = q.front();
q.pop();
for(int it = 0; it != g[nod].size(); ++it)
if(!f[g[nod][it]])
{
dist[g[nod][it]] = dist[nod] + 1;
f[g[nod][it]] = 1;
q.push(g[nod][it]);
}
}
for(int i=1; i<=n; ++i)
fout<<dist[i]<<" ";
return 0;
}