Pagini recente » Cod sursa (job #1678709) | Cod sursa (job #2164820) | Cod sursa (job #2878648) | Cod sursa (job #1971641) | Cod sursa (job #2858535)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pi pair<ll, ll>
#define sz(x) (int)((x).size())
#define all(a) (a).begin(), (a).end()
/* int f[maxn],nf[maxn],inv[maxn];
const int M=998244353;
void init(){
inv[1]=1; for (int i=2;i<maxn;i++) inv[i]=M-1ll*(M/i)*inv[M%i]%M;
f[0]=nf[0]=1; for (int i=1;i<maxn;i++) f[i]=1ll*f[i-1]*i%M,nf[i]=1ll*nf[i-1]*inv[i]%M;
}
int C(int x,int y){return 1ll*f[x]*nf[y]%M*nf[x-y]%M;}*/
const ll mod = 1e9+7;
ll n, k, m, mi, ma;
const ll N = 1e5 + 5;
vector<ll> ans;
vector<ll> a[N];
void solve(){
ifstream cin("bfs.in");
ofstream cout("bfs.out");
cin >> n >> m >> k;
for(int i = 0; i<m; i++){
ll x, y;
cin >> x >> y;
x--; y--;
a[x].pb(y);
}
for(int i = 0; i<n ;i++) ans.pb(mod);
k--;
ans[k] = 0;
queue<ll> q;
q.push(k);
vector<ll> v(n, 0);
while(!q.empty()){
ll x = q.front();
v[x] = 1;
q.pop();
for(int i = 0; i<a[x].size(); i++){
if(v[a[x][i]]) continue;
ans[a[x][i]] = min(ans[a[x][i]], ans[x] + 1);
q.push(a[x][i]);
v[a[x][i]] = 1;
}
}
for(int i = 0; i<n; i++) if(ans[i] == mod) cout << "-1 "; else cout << ans[i] << " "; cout << "\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
// cin >> t;
while(t--) solve();
return 0;
}