Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cstdint>
- #include <iostream>
- #include <queue>
- #include <set>
- #include <utility>
- #include <vector>
- int main() {
- int64_t stations, lineNum;
- std::cin >> stations >> lineNum;
- std::vector<std::set<int64_t>> lines(lineNum);
- for (int64_t i = 0; i < lineNum; ++i) {
- int64_t stInLine;
- std::cin >> stInLine;
- for (int64_t j = 0; j < stInLine; ++j) {
- int64_t curStat;
- std::cin >> curStat;
- lines[i].insert(curStat);
- }
- }
- int64_t from, to;
- std::cin >> from >> to;
- std::set<int64_t> reach = {from};
- for (int64_t i = 0; i < lineNum; ++i) {
- std::set<int64_t> newReach = reach;
- for (int64_t j = 0; j < lineNum; j++) {
- std::set<int64_t> inter;
- std::set_intersection(std::begin(lines[j]), std::end(lines[j]), std::begin(reach),
- std::end(reach), std::inserter(inter, std::begin(inter)));
- if (!inter.empty()) {
- std::set_union(std::begin(lines[j]), std::end(lines[j]), std::begin(newReach),
- std::end(newReach), std::inserter(newReach, std::begin(newReach)));
- }
- }
- reach = newReach;
- if (reach.find(to) != std::end(reach)) {
- std::cout << i << '\n';
- return 0;
- }
- }
- std::cout << -1 << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement