#include <bits/stdc++.h>
using namespace std;
int main(){
int n, q;
cin >> n >> q;
vector<pair<int, int> > inter(n);
for (int i = 0; i < n; i++){
cin >> inter[i].first >> inter[i].second;
}
vector<vector<int> > graph(n);
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (i == j) continue;
if (inter[i].second <= inter[j].second && inter[i].second >= inter[j].first){
graph[i].push_back(j);
}
}
}
// for (int i = 0; i < n; i++){
// for (int x : graph[i]) cout << x << " ";
// cout << endl;
// }
for (int i = 0; i < q; i++){
int s, e;
cin >> s >> e;
s--, e--;
queue<int> q;
q.push(s);
vector<int> tav(n, -1);
tav [s] = 0;
while(!q.empty()){
int pos = q.front();
q.pop();
for (int x : graph[pos]){
if (tav[x] == -1){
tav[x] = tav[pos] + 1;
q.push(x);
}
}
}
if (tav[e] == -1) {
cout << "IMPOSSIBLE" << endl;
}
else cout << tav[e] << endl;
}
return 0;
}
/*
5 2
1 3
2 4
4 7
7 9
3 7
1 4
3 2
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1546 ms |
3692 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |