Pagini recente » Cod sursa (job #935669) | Cod sursa (job #2291207) | Cod sursa (job #688148) | Cod sursa (job #2799059) | Cod sursa (job #3333479)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int N, M, S;
vector<int> adj[100005];
int v[100005], dist[100005];
queue<pair<int, int>> coada;
bool muchie(int x, int y){
for(int i = 0; i < adj[x].size(); i++)
if(adj[x][i] == y)
return true;
return false;
}
void bfs(int start){
coada.push({start, 0});
dist[start] = 0;
v[start] = 1;
while(!coada.empty()){
pair<int, int> s = coada.front();
coada.pop();
dist[s.first] = s.second;
for(int i = 1; i <= N; i++){
cout<<muchie(s.first, i)<<endl;
if(v[i] == 0 && muchie(s.first, i)){
v[i] = 1;
coada.push({i, s.second + 1});
}
}
}
for(int i = 1; i <= N; i++)
if(v[i] == 0)
g << -1 << " ";
else g << dist[i] << " ";
}
int main(){
f >> N >> M >> S;
for(int i = 1; i <= M; i++){
int x, y;
f >> x >> y;
adj[x].push_back(y);
}
bfs(S);
return 0;
}