Pagini recente » Cod sursa (job #1521644) | Istoria paginii utilizator/an3tjm | Rating Atodiresei Andrei Mihai (AndreiATO) | Istoria paginii runda/utcn_grafuri_2/clasament | Cod sursa (job #942079)
Cod sursa(job #942079)
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
vector <int> a[100005], cost(100005, -1);
queue <int> q;
int n, m, s, x, y;
int main()
{
in >> n >> m >> s;
cost [ s ]=0;
q.push ( s );
for(int i = 1;i <= m; ++i)
{
in >> x >> y;
a[ x ].push_back( y );//arc de la x la y
}
in.close();
while(!q.empty())
{
int b = q.back();
q.pop();
for(int i=0; i < a[b].size() ; ++i)
if(cost[a[b][i]]==-1)
{
cost[a[b][i]]=cost[b]+1;
q.push ( a[b][i] );
}
}
for(int i=1;i<=n;++i)
out<<cost[i]<<" ";
out.close();
return 0;
}