Pagini recente » Cod sursa (job #1439356) | Cod sursa (job #2342853) | Cod sursa (job #1956619) | Cod sursa (job #2555640) | Cod sursa (job #3243752)
#include <fstream>
#include <vector>
#include <map>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
ifstream cin("bfs.in");
ofstream cout("bfs.out");
#define ll long long
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
int n, m, s, xx, y;
vector<vector<int>> v;
int fr[100001];
void parc()
{
queue<pair<int, int>> q;
q.push({s, 1});
while(!q.empty())
{
pair<int, int> t = q.front();
q.pop();
for(auto &nod:v[t.first])
{
if(fr[nod] == 0)
{
q.push({nod, t.second + 1});
fr[nod] = t.second + 1;
}
}
}
for(int i = 1; i <= n; i++)
{
if(i == s)
cout<<"0"<<" ";
else
cout<<fr[i] - 1<<" ";
}
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
///ll t; cin>>t; while(t--) solve(), cout<<'\n';
cin>>n>>m>>s;
v.resize(n + 5);
for(int i = 1; i <= m; i++)
{
cin>>xx>>y;
v[xx].push_back(y);
}
parc();
return 0;
}