#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int> bfs(int source, vector<vector<int>> const& G) {
vector<int> dist(G.size(), -1);
queue<int> Q;
dist[source] = 0;
Q.push(source);
while (!Q.empty()) {
int v = Q.front(); Q.pop();
for (auto u : G[v]) {
if (dist[u] == -1) {
Q.push(u);
dist[u] = dist[v]+1;
}
}
}
return dist;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
vector<pair<int, int>> E(N);
for (auto &[x, y] : E) cin >> x >> y;
vector<vector<int>> G(N);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j) continue;
if (E[j].first <= E[i].second && E[i].second <= E[j].second)
G[i].push_back(j);
}
}
while (Q--) {
int v, u; cin >> v >> u; v--, u--;
auto dist = bfs(v, G);
if (dist[u] == -1) cout << "impossible" << endl;
else cout << dist[u] << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
1593 ms |
4588 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
1884 KB |
Output is correct |
7 |
Correct |
14 ms |
3784 KB |
Output is correct |
8 |
Correct |
14 ms |
5760 KB |
Output is correct |
9 |
Correct |
47 ms |
8536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
1884 KB |
Output is correct |
7 |
Correct |
14 ms |
3784 KB |
Output is correct |
8 |
Correct |
14 ms |
5760 KB |
Output is correct |
9 |
Correct |
47 ms |
8536 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
6 ms |
536 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
14 |
Correct |
5 ms |
464 KB |
Output is correct |
15 |
Correct |
8 ms |
1884 KB |
Output is correct |
16 |
Correct |
13 ms |
3676 KB |
Output is correct |
17 |
Correct |
15 ms |
5724 KB |
Output is correct |
18 |
Correct |
47 ms |
8540 KB |
Output is correct |
19 |
Execution timed out |
1581 ms |
55072 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
1884 KB |
Output is correct |
7 |
Correct |
14 ms |
3784 KB |
Output is correct |
8 |
Correct |
14 ms |
5760 KB |
Output is correct |
9 |
Correct |
47 ms |
8536 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
464 KB |
Output is correct |
14 |
Correct |
5 ms |
348 KB |
Output is correct |
15 |
Correct |
8 ms |
1884 KB |
Output is correct |
16 |
Correct |
18 ms |
3992 KB |
Output is correct |
17 |
Correct |
19 ms |
5724 KB |
Output is correct |
18 |
Correct |
47 ms |
8404 KB |
Output is correct |
19 |
Execution timed out |
1569 ms |
6528 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1533 ms |
4680 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
1593 ms |
4588 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |