Pagini recente » Cod sursa (job #1353406) | Cod sursa (job #781368) | Cod sursa (job #2142414) | Profil RobertJmek | Cod sursa (job #2212559)
#include <bits/stdc++.h>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int n,m,nr,lg[100002],s;
bool marker[100002];
vector <int > a[100002];
queue <int > q;
void functie_magica()
{
int x,y;
for(int i=1;i<=m;i++)
{
f>>x>>y;
a[x].push_back(y);
}
}
void functie_semimagica(int x)
{
int i,nod,y;
marker[x]=true;
q.push(x);
lg[x]=0;
while(!q.empty())
{
nod=q.front();
for(i=0;i<a[nod].size();i++)
{
y=a[nod][i];
if(!marker[y])
{
q.push(y);
marker[y]=true;
lg[y]=lg[nod]+1;
}
}
q.pop();
}
}
int main()
{
f>>n>>m>>s;
functie_magica();
for(int i=0;i<=n;i++)
{
lg[i]=-1;
}
functie_semimagica(s);
for(int i=1;i<=n;i++)
{
g<<lg[i]<<" ";
}
return 0;
}