Pagini recente » Cod sursa (job #852174) | Cod sursa (job #23604) | Cod sursa (job #2356129) | Cod sursa (job #284010) | Cod sursa (job #2849664)
#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;
int frq[MAX_SIZE] = {0};
while (!currentPositions.empty() && ok) {
int currentPoint = currentPositions.front();
int size = list[currentPoint].size();
for (int j = 0; j < size; ++j) {
if (frq[list[currentPoint][j]] == 0) {
if (list[currentPoint][j] == endPeak) {
ok = 0;
break;
}
frq[list[currentPoint][j]] = 1;
currentPositions.push_back(list[currentPoint][j]);
}
}
if (ok) {
currentPositions.pop_front();
}
++moves;
}
if (ok == 0) {
fout << moves - 1 << " ";
} else {
fout << -1 << " ";
}
}
}
/*
1
4
1 3 4 2
3
*/