Pagini recente » Cod sursa (job #2576770) | Cod sursa (job #1185408) | Cod sursa (job #29791) | Cod sursa (job #908827) | Cod sursa (job #2389556)
#if 1
#include <iostream>
#include <queue>
#include <vector>
#include <fstream>
using namespace std;
int viz[100001];
vector<int>graph[100001];
int a[1000001];
queue <int> myq;
void bfs(int nod)
{
a[nod] = 1;
viz[nod] = 1;
myq.push(nod);
while (!myq.empty())
{
int x = myq.front();
myq.pop();
//cout<<x<<" ";
int lim = graph[x].size();
for (int i = 0; i < lim; i++)
if (viz[graph[x][i]] == 0)
{
myq.push(graph[x][i]);
viz[graph[x][i]] = 1;
a[graph[x][i]] = a[x] + 1;
}
}
}
int main()
{
int n, m, s;
ifstream f("bfs.in");
ofstream g("bfs.out");
f >> n >> m >> s;
int k = 0;
for (int i = 1; i <= m; i++)
{
int a, b;
f >> a >> b;
graph[a].push_back(b);
// graph[b].push_back(a);
}
bfs(s);
for (int i = 1; i <= n; i++)
g << a[i] - 1 << " ";
return 0;
}
#endif