Pagini recente » Cod sursa (job #3190084) | Cod sursa (job #1781580) | Cod sursa (job #1027977) | Cod sursa (job #548244) | Cod sursa (job #2849653)
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <deque>
#include <vector>
#include <set>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
const int MAX_SIZE = 1000005;
vector<int> list[MAX_SIZE];
int main() {
int peaks, arrows, startPoint;
fin >> peaks >> arrows >> startPoint;
for (int i = 1; i <= arrows; ++i) {
int start, end;
fin >> start >> end;
list[start].push_back(end);
}
for (int endPeak = 1; endPeak <= peaks; ++endPeak) {
int moves = 0;
deque<int> currentPositions;
for (int j = 0; j < list[startPoint].size(); ++j) {
currentPositions.push_back(list[startPoint][j]);
}
int ok = 1;
while (!currentPositions.empty() && ok) {
++moves;
int currentPoint = currentPositions.front();
int size = list[currentPoint].size();
for (int j = 0; j < size; ++j) {
//cout << list[currentPoint][j] << " ";
if (list[currentPoint][j] != startPoint) {
if (list[currentPoint][j] == endPeak) {
ok = 0;
break;
}
currentPositions.push_back(list[currentPoint][j]);
}
}
if (ok) {
currentPositions.pop_front();
}
}
if (endPeak == startPoint) {
fout << 0 << " ";
} else if (ok == 0) {
fout << moves - 1 << " ";
} else {
fout << -1 << " ";
}
}
}
/*
1
4
1 3 4 2
3
*/