Pagini recente » Cod sursa (job #2284683) | Cod sursa (job #2190864) | Cod sursa (job #742566) | Cod sursa (job #1085393) | Cod sursa (job #3042326)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, sir[1004];
void functie(int mt[105][105], int poz, int contor, int initial) {
// cout << "contor = " << contor << endl;
//cout << "poz = " << poz << endl;
if(poz == initial) {
contor = 0;
}
for(int i = 1; i <= n; ++i) {
if(mt[poz][i] == 1) {
if(sir[i] > contor + 1 || sir[i] == -1) sir[i] = contor + 1;
mt[poz][i] = 0;
return functie(mt, i, contor + 1, initial);
}
}
return;
}
int main()
{
int m, s;
fin >> n >> m >> s;
for(int i = 1; i <= n; ++i) {
sir[i] = -1;
}
sir[s] = 0;
int mt[105][105];
int x, y;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j) {
mt[i][j] = 0;
}
}
for(int i = 0; i < m; ++i) {
fin >> x >> y;
mt[x][y] = 1;
}
functie(mt, s, 0, s);
for(int i = 1; i <= n; ++i) {
fout << sir[i] << " ";
}
// cout << endl;
// for(int i = 1; i <= n; ++i) {
// for(int j = 1; j <= n; ++j) {
// cout << mt[i][j] << " ";
// }
// cout << endl;
// }
return 0;
}