#include <bits/stdc++.h>
//#define int long long
using namespace std;
int N,Q,S[5001],E[5001],X,Y;
vector<int>ADJ[5001];
bool VIS[5001];
int DIST[5001][5001];
void BFS(int x,int DIS = 0){
for(int i = 0;i < N; i += 1)VIS[i] = 0;
queue<pair<int,int>>Q;
Q.push({x,0});
DIST[x][x] = 0;
VIS[x] = 1;
while(Q.size()){
int V = Q.front().first, S = Q.front().second;
Q.pop();
for(int i = 0; i <ADJ[V].size(); i += 1){
if(!VIS[ADJ[V][i]])
Q.push({ADJ[V][i], S + 1}),VIS[ADJ[V][i]] = 1,DIST[x][ADJ[V][i]] = S + 1;
}
}
}
int32_t main()
{
cin.tie(0),iostream::sync_with_stdio(0);
cin>>N>>Q;
for(int i = 0; i < N; i += 1)for(int l = 0; l < N; l += 1)DIST[i][l] = -1;
for(int i = 0; i < N; i += 1){
cin>>S[i]>>E[i];
}
for(int i = 0; i < N; i += 1){
for(int l = 0; l < N; l += 1){
if(S[l] <= E[i]&&E[i]<=E[l])
ADJ[i].push_back(l);
}
}
for(int i = 0; i < N; i += 1)BFS(i);
while(Q--){
cin>>X>>Y;
(DIST[X - 1][Y - 1] == -1?cout<<"impossible\n":cout<<DIST[X - 1][Y - 1]<<'\n');
}
}
Compilation message
events.cpp: In function 'void BFS(int, int)':
events.cpp:17:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int i = 0; i <ADJ[V].size(); i += 1){
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Runtime error |
158 ms |
199192 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
24 ms |
8404 KB |
Output is correct |
4 |
Correct |
16 ms |
8432 KB |
Output is correct |
5 |
Correct |
27 ms |
8436 KB |
Output is correct |
6 |
Correct |
58 ms |
9180 KB |
Output is correct |
7 |
Correct |
149 ms |
10004 KB |
Output is correct |
8 |
Correct |
180 ms |
11076 KB |
Output is correct |
9 |
Correct |
922 ms |
12420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
24 ms |
8404 KB |
Output is correct |
4 |
Correct |
16 ms |
8432 KB |
Output is correct |
5 |
Correct |
27 ms |
8436 KB |
Output is correct |
6 |
Correct |
58 ms |
9180 KB |
Output is correct |
7 |
Correct |
149 ms |
10004 KB |
Output is correct |
8 |
Correct |
180 ms |
11076 KB |
Output is correct |
9 |
Correct |
922 ms |
12420 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
488 KB |
Output is correct |
12 |
Correct |
27 ms |
8428 KB |
Output is correct |
13 |
Correct |
21 ms |
8444 KB |
Output is correct |
14 |
Correct |
26 ms |
8420 KB |
Output is correct |
15 |
Correct |
58 ms |
9216 KB |
Output is correct |
16 |
Correct |
151 ms |
10024 KB |
Output is correct |
17 |
Correct |
176 ms |
11092 KB |
Output is correct |
18 |
Correct |
973 ms |
12424 KB |
Output is correct |
19 |
Execution timed out |
1581 ms |
128384 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
24 ms |
8404 KB |
Output is correct |
4 |
Correct |
16 ms |
8432 KB |
Output is correct |
5 |
Correct |
27 ms |
8436 KB |
Output is correct |
6 |
Correct |
58 ms |
9180 KB |
Output is correct |
7 |
Correct |
149 ms |
10004 KB |
Output is correct |
8 |
Correct |
180 ms |
11076 KB |
Output is correct |
9 |
Correct |
922 ms |
12420 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
24 ms |
8404 KB |
Output is correct |
13 |
Correct |
21 ms |
8452 KB |
Output is correct |
14 |
Correct |
28 ms |
8420 KB |
Output is correct |
15 |
Correct |
62 ms |
9212 KB |
Output is correct |
16 |
Correct |
162 ms |
9940 KB |
Output is correct |
17 |
Correct |
175 ms |
11096 KB |
Output is correct |
18 |
Correct |
1031 ms |
12416 KB |
Output is correct |
19 |
Runtime error |
198 ms |
199140 KB |
Execution killed with signal 11 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
176 ms |
199140 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Runtime error |
158 ms |
199192 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |