Pagini recente » Cod sursa (job #1307563) | Cod sursa (job #1002996) | Cod sursa (job #1733417) | Cod sursa (job #1587433) | Cod sursa (job #2782980)
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
/*const long long mod = 998244353;
long long fct[200005],invfct[200005],inv[200005];
long long put2[200005];
long long lgput (long long a, long long exp)
{
long long rz=1;
while(exp)
if(exp&1)
exp^=1,rz=rz*1LL*a%mod;
else
exp>>=1,a=a*1LL*a%mod;
return rz;
}
long long cmb (long long n, long long k)
{
if(n<k || k<0 || n<0)
return 0;
return fct[n]*1LL*invfct[k]%mod*1LL*invfct[n-k]%mod;
}
void init ()
{
inv[1]=fct[0]=fct[1]=invfct[0]=invfct[1]=put2[0]=1,put2[1]=2;
for(long long i=2;i<=200000;++i)
put2[i]=put2[i-1]*2LL%mod,inv[i]=(mod-mod/i)*1LL*inv[mod%i]%mod,fct[i]=i*1LL*fct[i-1]%mod,invfct[i]=inv[i]*1LL*invfct[i-1]%mod;
}
long long v[200005];*/
vector<int> v[100002];
int rz[100002];
queue<int> q;
void solve()
{
int n,m,s;
f>>n>>m>>s;
for(int i=1;i<=m;i++)
{
int x,y;
f>>x>>y;
v[x].push_back(y);
//v[y].push_back(x);
}
for(int i=1;i<=n;i++)
rz[i]=-1;
q.push(s);
rz[s]=0;
while(!q.empty())
{
int nod=q.front();
q.pop();
for(auto i:v[nod])
{
if(rz[i]==-1)
rz[i]=rz[nod]+1,q.push(i);
}
}
for(int i=1;i<=n;i++)
g<<rz[i]<<" ";
}
int main()
{
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
/*int t;
cin>>t;
while(t--)*/
solve();
return 0;
}