Pagini recente » Cod sursa (job #2543695) | Cod sursa (job #138898) | Cod sursa (job #2882471) | Cod sursa (job #2978745) | Cod sursa (job #2487585)
#include <bits/stdc++.h>
#define NMAX 100001
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int dist[NMAX];
vector<int> graph[NMAX];
queue<pair<int, int>> q;
int main()
{
int n,m,s;
fin >> n >> m >> s;
while(m--)
{
int x, y;
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
for(int i = 1; i <= n; i++) dist[i] = -1;
q.push({s, 0});
while(!q.empty())
{
int node = q.front().first;
int d = q.front().second;
dist[node] = d;
q.pop();
for(auto x : graph[node])
{
if(dist[x] == -1)
{
q.push({x, d+1});
}
}
}
for(int i = 1; i <= n; i++) fout << dist[i] << ' ';
fin.close();
fout.close();
return 0;
}