Pagini recente » Istoria paginii runda/ultimaadmine/clasament | Rating Iacobescu Cristi (cristy_hereg93) | Istoria paginii runda/a_b/clasament | Cod sursa (job #1075259) | Cod sursa (job #662954)
Cod sursa(job #662954)
# include <fstream>
# include <vector>
# include <queue>
# define Nmax 100001
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
vector<int> v[Nmax];
queue <int> Q;
int n,m,s,x,y,cost[Nmax];
int main()
{
f>>n>>m>>s;
for (int i = 1; i <= m; ++i)
{f>>x>>y; v[x].push_back(y);}
for (int i = 1; i <= n; ++i) cost[i]=-1;
Q.push(s); cost[s]=0;
while( !Q.empty() ) //3
{x=Q.front(); Q.pop(); //4
for(unsigned int i=0; i<v[x].size(); ++i ) //5
{y=v[x][i];
if( cost[y]==-1 )
{Q.push(y); cost[y]=cost[x]+1;}
}
}
for (int i = 1; i <= n; ++i) g<<cost[i]<<' ';
g<<'\n';
return 0;
}