Pagini recente » Cod sursa (job #676551) | Cod sursa (job #406419) | Cod sursa (job #1473050) | Cod sursa (job #2063946) | Cod sursa (job #2487760)
#include <fstream>
#include <vector>
#include <bits/stdc++.h>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n , m , x , y , s;
queue <int> Q;
vector <int> G[100001];
bool viz[100001];
int lg[100001];
void bfs(int nod)
{
Q.push(nod);
while(!Q.empty())
{
int curent = Q.front();
Q.pop();
for(vector <int> :: iterator it = G[curent].begin() ; it != G[curent].end() ; it ++)
if(!viz[*it])
{
lg[*it] = lg[curent] + 1;
viz[*it] = 1;
Q.push(*it);
}
}
}
int main()
{
fin >> n >> m >> s;
for(int i = 1 ; i <= m ; i ++)
{
fin >> x >> y;
G[x].push_back(y);
}
viz[s] = 1;
bfs(s);
for(int i = 1 ; i <= n ; i ++)
if(i != s && lg[i] == 0)
fout << -1 << ' ';
else if(i == s)
fout << 0 << ' ';
else
fout << lg[i] << ' ';
fin.close();
fout.close();
return 0;
}