#include<bits/stdc++.h>
#define pb push_back
#define sz(x) (int)(x.size())
#define all(x) x.begin(), x.end()
#define int long long
#define pii pair<int, int>
#define inf 1e9
#define mod 1000000007
#define F first
#define S second
#define wopen(x) freopen((x),"w",stdout)
#define ropen(x) freopen((x),"r",stdin)
#define de(x) cout << #x << " = " << x << ' '
#define IO ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
const int mxN = 3e4 + 5;
int n, m, b[mxN], p[mxN];
bitset<mxN * mxN> vis;
bitset<mxN> used;
vector<int> v[mxN];
signed main() {
IO;
cin >> n >> m;
for(int i = 0; i < m; i++) cin >> b[i] >> p[i], v[b[i]].pb(p[i]);
deque<array<int, 3>> q;
q.pb({b[0], p[0], 0});
while(!q.empty()) {
auto now = q.front(); q.pop_front();
vis[now[0] * n + now[1]] = 1;
if(now[0] == b[1]) {
cout << now[2] << '\n';
return 0;
}
if(!used[now[0]]) {
for(int i : v[now[0]]) {
if(vis[now[0] * n + i]) continue;
vis[now[0] * n + i] = 1;
q.push_front({now[0], i, now[2]});
}
}
used[now[0]] = 1;
if(now[0] + now[1] < n and vis[(now[0] + now[1]) * n + now[1]] == 0)
q.pb({now[0] + now[1], now[1], now[2] + 1});
if(now[0] - now[1] >= 0 and vis[(now[0] - now[1]) * n + now[1]] == 0)
q.pb({now[0] - now[1], now[1], now[2] + 1});
}
cout << -1 << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
1 ms |
1004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
1 ms |
1004 KB |
Output is correct |
8 |
Correct |
1 ms |
1004 KB |
Output is correct |
9 |
Correct |
1 ms |
1004 KB |
Output is correct |
10 |
Correct |
1 ms |
1132 KB |
Output is correct |
11 |
Correct |
1 ms |
1132 KB |
Output is correct |
12 |
Correct |
2 ms |
1152 KB |
Output is correct |
13 |
Correct |
1 ms |
1132 KB |
Output is correct |
14 |
Correct |
1 ms |
1132 KB |
Output is correct |
15 |
Correct |
2 ms |
1132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
1 ms |
1004 KB |
Output is correct |
8 |
Correct |
1 ms |
1004 KB |
Output is correct |
9 |
Correct |
1 ms |
1004 KB |
Output is correct |
10 |
Correct |
1 ms |
1132 KB |
Output is correct |
11 |
Correct |
1 ms |
1132 KB |
Output is correct |
12 |
Correct |
1 ms |
1132 KB |
Output is correct |
13 |
Correct |
2 ms |
1132 KB |
Output is correct |
14 |
Correct |
1 ms |
1132 KB |
Output is correct |
15 |
Correct |
1 ms |
1132 KB |
Output is correct |
16 |
Correct |
1 ms |
1132 KB |
Output is correct |
17 |
Correct |
2 ms |
1260 KB |
Output is correct |
18 |
Correct |
1 ms |
1132 KB |
Output is correct |
19 |
Correct |
1 ms |
1132 KB |
Output is correct |
20 |
Correct |
2 ms |
1644 KB |
Output is correct |
21 |
Correct |
1 ms |
1132 KB |
Output is correct |
22 |
Correct |
1 ms |
1132 KB |
Output is correct |
23 |
Correct |
2 ms |
1388 KB |
Output is correct |
24 |
Correct |
2 ms |
1644 KB |
Output is correct |
25 |
Correct |
2 ms |
1644 KB |
Output is correct |
26 |
Correct |
3 ms |
1644 KB |
Output is correct |
27 |
Correct |
2 ms |
1644 KB |
Output is correct |
28 |
Correct |
3 ms |
1644 KB |
Output is correct |
29 |
Correct |
3 ms |
1644 KB |
Output is correct |
30 |
Correct |
2 ms |
1516 KB |
Output is correct |
31 |
Correct |
2 ms |
1644 KB |
Output is correct |
32 |
Correct |
2 ms |
1644 KB |
Output is correct |
33 |
Correct |
4 ms |
1644 KB |
Output is correct |
34 |
Correct |
3 ms |
1644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
1 ms |
1004 KB |
Output is correct |
8 |
Correct |
1 ms |
1004 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1132 KB |
Output is correct |
11 |
Correct |
1 ms |
1132 KB |
Output is correct |
12 |
Correct |
1 ms |
1132 KB |
Output is correct |
13 |
Correct |
1 ms |
1132 KB |
Output is correct |
14 |
Correct |
2 ms |
1132 KB |
Output is correct |
15 |
Correct |
2 ms |
1132 KB |
Output is correct |
16 |
Correct |
1 ms |
1132 KB |
Output is correct |
17 |
Correct |
2 ms |
1260 KB |
Output is correct |
18 |
Correct |
1 ms |
1132 KB |
Output is correct |
19 |
Correct |
2 ms |
1132 KB |
Output is correct |
20 |
Correct |
2 ms |
1664 KB |
Output is correct |
21 |
Correct |
1 ms |
1132 KB |
Output is correct |
22 |
Correct |
1 ms |
1132 KB |
Output is correct |
23 |
Correct |
2 ms |
1388 KB |
Output is correct |
24 |
Correct |
2 ms |
1644 KB |
Output is correct |
25 |
Correct |
2 ms |
1644 KB |
Output is correct |
26 |
Correct |
2 ms |
1644 KB |
Output is correct |
27 |
Correct |
2 ms |
1644 KB |
Output is correct |
28 |
Correct |
2 ms |
1644 KB |
Output is correct |
29 |
Correct |
3 ms |
1644 KB |
Output is correct |
30 |
Correct |
2 ms |
1516 KB |
Output is correct |
31 |
Correct |
2 ms |
1644 KB |
Output is correct |
32 |
Correct |
2 ms |
1644 KB |
Output is correct |
33 |
Correct |
4 ms |
1644 KB |
Output is correct |
34 |
Correct |
3 ms |
1644 KB |
Output is correct |
35 |
Correct |
8 ms |
2412 KB |
Output is correct |
36 |
Correct |
2 ms |
1388 KB |
Output is correct |
37 |
Correct |
6 ms |
2540 KB |
Output is correct |
38 |
Correct |
8 ms |
2668 KB |
Output is correct |
39 |
Correct |
7 ms |
2028 KB |
Output is correct |
40 |
Correct |
9 ms |
2412 KB |
Output is correct |
41 |
Correct |
9 ms |
2560 KB |
Output is correct |
42 |
Correct |
8 ms |
2412 KB |
Output is correct |
43 |
Correct |
6 ms |
2412 KB |
Output is correct |
44 |
Correct |
9 ms |
2452 KB |
Output is correct |
45 |
Correct |
16 ms |
3180 KB |
Output is correct |
46 |
Correct |
11 ms |
3180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
2 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
1 ms |
1004 KB |
Output is correct |
8 |
Correct |
1 ms |
1004 KB |
Output is correct |
9 |
Correct |
1 ms |
1004 KB |
Output is correct |
10 |
Correct |
1 ms |
1132 KB |
Output is correct |
11 |
Correct |
1 ms |
1152 KB |
Output is correct |
12 |
Correct |
1 ms |
1132 KB |
Output is correct |
13 |
Correct |
1 ms |
1132 KB |
Output is correct |
14 |
Correct |
2 ms |
1132 KB |
Output is correct |
15 |
Correct |
2 ms |
1132 KB |
Output is correct |
16 |
Correct |
1 ms |
1132 KB |
Output is correct |
17 |
Correct |
2 ms |
1260 KB |
Output is correct |
18 |
Correct |
2 ms |
1132 KB |
Output is correct |
19 |
Correct |
1 ms |
1132 KB |
Output is correct |
20 |
Correct |
2 ms |
1644 KB |
Output is correct |
21 |
Correct |
1 ms |
1132 KB |
Output is correct |
22 |
Correct |
1 ms |
1132 KB |
Output is correct |
23 |
Correct |
2 ms |
1388 KB |
Output is correct |
24 |
Correct |
2 ms |
1644 KB |
Output is correct |
25 |
Correct |
2 ms |
1644 KB |
Output is correct |
26 |
Correct |
2 ms |
1644 KB |
Output is correct |
27 |
Correct |
2 ms |
1644 KB |
Output is correct |
28 |
Correct |
3 ms |
1644 KB |
Output is correct |
29 |
Correct |
3 ms |
1644 KB |
Output is correct |
30 |
Correct |
2 ms |
1516 KB |
Output is correct |
31 |
Correct |
2 ms |
1644 KB |
Output is correct |
32 |
Correct |
2 ms |
1644 KB |
Output is correct |
33 |
Correct |
4 ms |
1644 KB |
Output is correct |
34 |
Correct |
3 ms |
1644 KB |
Output is correct |
35 |
Correct |
8 ms |
2412 KB |
Output is correct |
36 |
Correct |
3 ms |
1388 KB |
Output is correct |
37 |
Correct |
7 ms |
2540 KB |
Output is correct |
38 |
Correct |
9 ms |
2668 KB |
Output is correct |
39 |
Correct |
8 ms |
2028 KB |
Output is correct |
40 |
Correct |
8 ms |
2412 KB |
Output is correct |
41 |
Correct |
8 ms |
2540 KB |
Output is correct |
42 |
Correct |
6 ms |
2412 KB |
Output is correct |
43 |
Correct |
6 ms |
2412 KB |
Output is correct |
44 |
Correct |
6 ms |
2452 KB |
Output is correct |
45 |
Correct |
17 ms |
3180 KB |
Output is correct |
46 |
Correct |
11 ms |
3180 KB |
Output is correct |
47 |
Correct |
15 ms |
9324 KB |
Output is correct |
48 |
Correct |
7 ms |
1900 KB |
Output is correct |
49 |
Correct |
7 ms |
1900 KB |
Output is correct |
50 |
Correct |
5 ms |
1644 KB |
Output is correct |
51 |
Correct |
77 ms |
82028 KB |
Output is correct |
52 |
Correct |
90 ms |
90092 KB |
Output is correct |
53 |
Correct |
23 ms |
18924 KB |
Output is correct |
54 |
Correct |
48 ms |
61292 KB |
Output is correct |
55 |
Correct |
59 ms |
77932 KB |
Output is correct |
56 |
Correct |
91 ms |
112592 KB |
Output is correct |
57 |
Correct |
3 ms |
2668 KB |
Output is correct |
58 |
Correct |
89 ms |
111984 KB |
Output is correct |
59 |
Correct |
87 ms |
104176 KB |
Output is correct |
60 |
Correct |
85 ms |
104176 KB |
Output is correct |
61 |
Correct |
81 ms |
99052 KB |
Output is correct |
62 |
Correct |
141 ms |
112564 KB |
Output is correct |
63 |
Correct |
240 ms |
112492 KB |
Output is correct |
64 |
Correct |
248 ms |
112492 KB |
Output is correct |
65 |
Correct |
323 ms |
112748 KB |
Output is correct |
66 |
Correct |
513 ms |
112876 KB |
Output is correct |
67 |
Correct |
316 ms |
112748 KB |
Output is correct |