Untitled
unknown
plain_text
a year ago
2.1 kB
11
Indexable
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
// Function to determine if travel is possible
string canTravel(int n, int m, vector<int>& routeA, vector<int>& routeB, int x, int y) {
// Convert routes to unordered sets for faster lookup
unordered_set<int> setA(routeA.begin(), routeA.end());
unordered_set<int> setB(routeB.begin(), routeB.end());
// Check if both source and destination are in the same route
if ((setA.count(x) && setA.count(y)) || (setB.count(x) && setB.count(y))) {
return "Yes";
}
// Check for intersection points
for (int stop : routeA) {
if (setB.count(stop)) {
// If there's an intersection, and either x is in routeA and y in routeB, or vice versa
if ((setA.count(x) && setB.count(y)) || (setB.count(x) && setA.count(y))) {
return "Yes";
}
}
}
// If none of the above conditions are met, return No
return "No";
}
int main() {
int n, m, x, y;
// Input for number of stops in Route A and B
cin >> n >> m;
vector<int> routeA(n);
vector<int> routeB(m);
// Input stops for Route A
for (int i = 0; i < n; ++i) {
cin >> routeA[i];
}
// Input stops for Route B
for (int i = 0; i < m; ++i) {
cin >> routeB[i];
}
// Input source and destination stops
cin >> x >> y;
// Determine if travel is possible and print the result
cout << canTravel(n, m, routeA, routeB, x, y) << endl;
return 0;
}
/*
Sample Input 1:
5 6
1 2 3 4 5
6 7 8 9 10
2 9
Sample Output 1:
Yes
Sample Input 2:
4 4
11 22 33 44
55 66 33 77
11 66
Sample Output 2:
No
Explanation for Sample Input 1:
Both routes intersect (meet) at bus stop number 3. So, the passenger can travel from Stop 2 to Stop 9 using the available buses.
Explanation for Sample Input 2:
There is no common stop where both routes intersect. So, there is no continuous bus route from Stop 11 to Stop 66, and the passenger cannot travel directly using the available buses.
*/
Editor is loading...
Leave a Comment