Pagini recente » lol_asd | Cod sursa (job #634926) | Cod sursa (job #211848) | Cod sursa (job #2052370) | Cod sursa (job #2578612)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
vector<int> muchie[200005];
int n,m,s;
bool viz[200005];
int coada[200005],dist[200005];
void bfs()
{
int p=1,u=1;
while(p<=u)
{
int nodcrt=coada[p];
for(int j=0;j<muchie[nodcrt].size();j++)
{
if(viz[muchie[nodcrt][j]]==0)
{
coada[++u]=muchie[nodcrt][j];
dist[muchie[nodcrt][j]]=dist[nodcrt]+1;
viz[muchie[nodcrt][j]]=1;
}
}
p++;
}
}
int main()
{
f>>n>>m>>s;
for(int i=1;i<=m;i++)
{
int x,y;
f>>x>>y;
if(x!=y)
muchie[x].push_back(y);
}
viz[s]=1;
for(int i=1;i<=n;i++)
{
dist[i]=-1;
}
dist[s]=0;
coada[1]=s;
bfs();
for(int i=1;i<=n;i++)
{
g<<dist[i]<<" ";
}
}