Pagini recente » Cod sursa (job #1691409) | Cod sursa (job #939324) | Cod sursa (job #1025104) | Cod sursa (job #2435431) | Cod sursa (job #1315300)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
const int N = 100001;
vector <int> a[N];
int n,m,s;
queue <int> q;
int cost[N];
void citire()
{
int i, x, y;
in >> n >> m >> s;
for (i = 0; i < m; i++)
{
in >> x >> y;
a[x].push_back(y);
}
}
/*
parcurg lista de adiacenta a lui x:
for (size_t i = 0; i < a[x].size(); i++)
{
y = a[x][i];//vecinul (succesorul) curent
prelucrez y:
}
*/
int main()
{
citire();
for(int i = 1; i <= n; i++)
cost[i] = -1;
cost[s] = 0;
q.push(s);
while(!q.empty())
{
int x = q.front();
q.pop();
for(size_t i = 0; i < a[x].size(); i++)
{
int y = a[x][i];
if(cost[y] == -1)
{
cost[y] = cost[x] + 1;
q.push(y);
}
}
}
for(int i = 1; i <= n; i++)
out << cost[i] << ' ';
out << '\n';
return 0;
}