#include <iostream>
#include <vector>
using namespace std;
struct event {
int s, e;
};
int n, q, a, b;
vector <event> v(5001);
vector <vector <int>> g(1e5 + 1);
vector <vector <int>> d(5001, vector <int>(5001, 1e9));
vector <bool> done(5001, false);
void dfs(int x) {
if (done[x]) return;
done[x] = true;
for (int i : g[x]) {
dfs(i);
for (int j = 1; j <= n; j++) d[x][j] = min(d[x][j], d[i][j] + 1);
}
}
int main()
{
cin >> n >> q;
if (n > 5000) return 0;
for (int i = 1; i <= n; i++) {
cin >> v[i].s >> v[i].e;
d[i][i] = 0;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (v[i].e >= v[j].s && v[i].e < v[j].e) g[i].push_back(j);
if (v[i].e == v[j].e && v[i].s < v[j].s) g[i].push_back(j);
}
}
for (int i = 1; i <= n; i++) dfs(i);
for (int i = 0; i < q; i++) {
cin >> a >> b;
if (d[a][b] < 1e9) cout << d[a][b] << "\n";
else cout << "impossible\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
100688 KB |
Output is correct |
2 |
Incorrect |
43 ms |
100784 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
100684 KB |
Output is correct |
2 |
Correct |
43 ms |
100752 KB |
Output is correct |
3 |
Correct |
53 ms |
100812 KB |
Output is correct |
4 |
Correct |
50 ms |
100684 KB |
Output is correct |
5 |
Correct |
51 ms |
100684 KB |
Output is correct |
6 |
Correct |
199 ms |
101516 KB |
Output is correct |
7 |
Correct |
386 ms |
102356 KB |
Output is correct |
8 |
Correct |
607 ms |
103480 KB |
Output is correct |
9 |
Incorrect |
49 ms |
100672 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
100684 KB |
Output is correct |
2 |
Correct |
43 ms |
100752 KB |
Output is correct |
3 |
Correct |
53 ms |
100812 KB |
Output is correct |
4 |
Correct |
50 ms |
100684 KB |
Output is correct |
5 |
Correct |
51 ms |
100684 KB |
Output is correct |
6 |
Correct |
199 ms |
101516 KB |
Output is correct |
7 |
Correct |
386 ms |
102356 KB |
Output is correct |
8 |
Correct |
607 ms |
103480 KB |
Output is correct |
9 |
Incorrect |
49 ms |
100672 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
100684 KB |
Output is correct |
2 |
Correct |
43 ms |
100752 KB |
Output is correct |
3 |
Correct |
53 ms |
100812 KB |
Output is correct |
4 |
Correct |
50 ms |
100684 KB |
Output is correct |
5 |
Correct |
51 ms |
100684 KB |
Output is correct |
6 |
Correct |
199 ms |
101516 KB |
Output is correct |
7 |
Correct |
386 ms |
102356 KB |
Output is correct |
8 |
Correct |
607 ms |
103480 KB |
Output is correct |
9 |
Incorrect |
49 ms |
100672 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
46 ms |
100692 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
100688 KB |
Output is correct |
2 |
Incorrect |
43 ms |
100784 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |