Pagini recente » Monitorul de evaluare | Cod sursa (job #1149001) | Monitorul de evaluare | Cod sursa (job #1932730) | Cod sursa (job #1705825)
#include <vector>
#include <fstream>
#include <queue>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
vector <int> graph[100005];
vector <bool> viz(100005, false);
vector <int> dist(100005, -1);
queue <int> q;
int main(){
int n, m, s;
f >> n >> m >> s;
dist[s] = 0;
q.push(s);
viz[s] = true;
int x, y;
for(int i = 1; i <= m; i++){
f >> x >> y;
graph[x].push_back(y);
}
int node, son;
while(!q.empty()){
node = q.front();
q.pop();
for(int i = 0; i < graph[node].size(); i++){
son = graph[node][i];
if(!viz[son]){
dist[son] = dist[node] + 1;
viz[son] = true;
q.push(son);
}
}
}
for(int i = 1; i <= n; i++)
g << dist[i] << " ";
}