Pagini recente » Profil 05_Yohn | Cod sursa (job #1115550) | Cod sursa (job #1371096) | Cod sursa (job #3359222) | Cod sursa (job #3304724)
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");
vector<int> graph[100001];
int cost[100001];
void bfs(int s)
{
queue<int> q;
q.push(s);
cost[s] = 1;
while (!q.empty())
{
int x = q.front();
q.pop();
for (auto i : graph[x])
{
cout << i;
if (cost[i] == 0)
{
cost[i] = cost[x] + 1;
q.push(i);
}
}
}
}
int main()
{
int n, m, s;
fin >> n >> m >> s;
while (m--)
{
int x, y;
fin >> x >> y;
graph[x].push_back(y);
}
bfs(s);
for (int i = 1; i <= n; i++)
{
fout << cost[i] - 1 << " ";
}
return 0;
}