#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define Yungyaorz ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pii pair <int, int>
#define pll pair <long long int, long long int>
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define F first
#define S second
#define debug(x) cerr << #x << '=' << x << '\n'
typedef long long ll;
const int N = 305;
set <int> graph[N];
signed main() {
Yungyaorz
int n, k, m;
cin >> n >> k >> m;
for(int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
if(a > b) {
for(int u = a; u >= max(a - k + 1, b + 1); --u) {
for(int v = u - 1; v >= b; --v) {
graph[u].insert(v);
}
}
}
else { // a < b
for(int u = a; u <= min(a + k - 1, b - 1); ++u) {
for(int v = u + 1; v <= b; ++v) {
graph[u].insert(v);
}
}
}
}
int q;
cin >> q;
while(q--) {
int s, t;
cin >> s >> t;
vector <int> dis(n + 1);
queue <int> que;
que.push(s);
dis[s] = 0;
bool flag = false;
while(que.size()) {
int u = que.front();
que.pop();
for(auto v : graph[u]) {
if(dis[v]) continue;
dis[v] = dis[u] + 1;
if(v == t) {
flag = true;
break;
}
que.push(v);
}
if(flag) break;
}
if(!flag) cout << -1 << '\n';
else cout << dis[t] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
980 KB |
Output is correct |
2 |
Correct |
24 ms |
1296 KB |
Output is correct |
3 |
Correct |
5 ms |
2260 KB |
Output is correct |
4 |
Correct |
50 ms |
2260 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
161 ms |
4464 KB |
Output is correct |
7 |
Correct |
48 ms |
4104 KB |
Output is correct |
8 |
Correct |
58 ms |
4412 KB |
Output is correct |
9 |
Correct |
52 ms |
4320 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
980 KB |
Output is correct |
2 |
Correct |
24 ms |
1296 KB |
Output is correct |
3 |
Correct |
5 ms |
2260 KB |
Output is correct |
4 |
Correct |
50 ms |
2260 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
161 ms |
4464 KB |
Output is correct |
7 |
Correct |
48 ms |
4104 KB |
Output is correct |
8 |
Correct |
58 ms |
4412 KB |
Output is correct |
9 |
Correct |
52 ms |
4320 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
14 |
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 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
6 ms |
3424 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
980 KB |
Output is correct |
2 |
Correct |
24 ms |
1296 KB |
Output is correct |
3 |
Correct |
5 ms |
2260 KB |
Output is correct |
4 |
Correct |
50 ms |
2260 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
161 ms |
4464 KB |
Output is correct |
7 |
Correct |
48 ms |
4104 KB |
Output is correct |
8 |
Correct |
58 ms |
4412 KB |
Output is correct |
9 |
Correct |
52 ms |
4320 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |