Pagini recente » Cod sursa (job #1270552) | Cod sursa (job #1084097) | Cod sursa (job #665328) | Cod sursa (job #1013768) | Cod sursa (job #2325749)
#include<bits/stdc++.h>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
const int Nmax = 100005;
queue<int>Q;
bitset<Nmax> viz;
list<int> G[Nmax];
list<int>::iterator it;
int n, m, start, i, nod_curent, dist[Nmax], x ,y;
void citire() {
f >> n >> m >> start;
while(m--) {
f >> x >> y;
G[x].push_back(y);
}
}
void BFS() {
Q.push(start);
viz[start] = true;
for(i = 1; i <= n; ++i)
dist[i] = -1;
dist[start] = 0;
while(!Q.empty()) {
nod_curent = Q.front();
for(it = G[nod_curent].begin(); it != G[nod_curent].end(); ++it)
if(!viz[*it]){
Q.push(*it);
viz[*it] = true;
dist[*it] = dist[nod_curent] + 1;
}
Q.pop();
}
}
int main() {
citire();
BFS();
for(i = 1; i <= n; ++i)
g << dist[i] << ' ';
}