Pagini recente » Cod sursa (job #2462383) | Cod sursa (job #2255601) | Cod sursa (job #1105735) | Cod sursa (job #2876462) | Cod sursa (job #2781505)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define VMAX 100000
using namespace std;
int V, E, x, y, src;
vector <int> adj[VMAX];
bool vis[VMAX];
int d[VMAX];
ifstream fin("bfs.in");
ofstream fout("bfs.out");
void BFS(int src)
{
int u;
queue <int> q;
q.push(src);
d[src] = 1;
while (!q.empty())
{
u = q.front();
q.pop();
for (auto w:adj[u])
if (!d[w])
{
d[w] = d[u] + 1;
q.push(w);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
fin.tie(0);
fout.tie(0);
fin >> V >> E >> src;
while (E--)
{
fin >> x >> y;
adj[x - 1].push_back(y - 1);
}
BFS(src - 1);
for (int i = 0; i < V; ++i)
fout << d[i] - 1 << " ";
fin.close();
fout.close();
return 0;
}