#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define MAX 1005
int n, q;
pair<int, int>events[MAX];
bool check[MAX];
int dists[MAX][MAX];
vector<int>g[MAX];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
for(int i = 1;i <= n;++i){
cin >> events[i].first >> events[i].second;
}
for(int i = 1;i <= n;++i){
for(int j = 1;j <= n;++j){
if(i == j)continue;
if(events[i].second >= events[j].first && events[i].second <= events[j].second){
g[i].push_back(j);
}
}
}
// for(int i = 1;i <= n;++i){
// cout << i << ": ";
// for(int j : g[i]){
// cout << j << " ";
// }
// cout << '\n';
// }
for(int i = 1;i <= n;++i){
for(int j = 1;j <= n;++j){
dists[i][j] = -1;
}
}
for(int i = 1;i <= n;++i)check[i] = 0;
for(int i = 0;i < q;++i){
int u, v;
cin >> u >> v;
if(dists[u][v] != -1)cout << dists[u][v] << '\n';
else if(dists[u][v] == -1 && check[u])cout << "impossible\n";
else{
queue<int>que;
que.push(u);
check[u] = 1;
dists[u][u] = 0;
while(!que.empty()){
int cur = que.front();
que.pop();
for(int vv : g[cur]){
if(dists[u][vv] == -1){
dists[u][vv] = dists[u][cur] + 1;
que.push(vv);
}
}
}
if(dists[u][v] == -1)cout << "impossible\n";
else cout << dists[u][v] << '\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
7 ms |
4224 KB |
Output is correct |
4 |
Correct |
7 ms |
4308 KB |
Output is correct |
5 |
Correct |
7 ms |
4308 KB |
Output is correct |
6 |
Correct |
13 ms |
5108 KB |
Output is correct |
7 |
Correct |
22 ms |
5844 KB |
Output is correct |
8 |
Correct |
25 ms |
6868 KB |
Output is correct |
9 |
Correct |
8 ms |
8276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
7 ms |
4224 KB |
Output is correct |
4 |
Correct |
7 ms |
4308 KB |
Output is correct |
5 |
Correct |
7 ms |
4308 KB |
Output is correct |
6 |
Correct |
13 ms |
5108 KB |
Output is correct |
7 |
Correct |
22 ms |
5844 KB |
Output is correct |
8 |
Correct |
25 ms |
6868 KB |
Output is correct |
9 |
Correct |
8 ms |
8276 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
7 ms |
4280 KB |
Output is correct |
13 |
Correct |
6 ms |
4308 KB |
Output is correct |
14 |
Correct |
7 ms |
4308 KB |
Output is correct |
15 |
Correct |
12 ms |
5076 KB |
Output is correct |
16 |
Correct |
26 ms |
5812 KB |
Output is correct |
17 |
Correct |
31 ms |
6868 KB |
Output is correct |
18 |
Correct |
9 ms |
8276 KB |
Output is correct |
19 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
7 ms |
4224 KB |
Output is correct |
4 |
Correct |
7 ms |
4308 KB |
Output is correct |
5 |
Correct |
7 ms |
4308 KB |
Output is correct |
6 |
Correct |
13 ms |
5108 KB |
Output is correct |
7 |
Correct |
22 ms |
5844 KB |
Output is correct |
8 |
Correct |
25 ms |
6868 KB |
Output is correct |
9 |
Correct |
8 ms |
8276 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
7 ms |
4228 KB |
Output is correct |
13 |
Correct |
6 ms |
4308 KB |
Output is correct |
14 |
Correct |
7 ms |
4308 KB |
Output is correct |
15 |
Correct |
12 ms |
5088 KB |
Output is correct |
16 |
Correct |
23 ms |
5924 KB |
Output is correct |
17 |
Correct |
27 ms |
6972 KB |
Output is correct |
18 |
Correct |
8 ms |
8320 KB |
Output is correct |
19 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |