#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
bitset<30001> bs,bs2[30001];
vector<int> v[30001];
int n,m,tag,tag2;
int main(){
ios_base::sync_with_stdio(false);
cout.tie(NULL);
cin.tie(NULL);
cin >> n >> m;
for(int i = 0 ; i < m ; i += 1){
int a,b;
cin >> a >> b;
if(i == 0){
tag2 = a;
}else if(i == 1){
tag = a;
}
v[a].push_back(b);
}
queue<pair<int,int>> q;
for(auto i : v[tag2]){
q.push({tag2,i});
bs2[tag2][i] = 1;
}
bs[tag2] = 1;
int dist = 0;
while(q.size()){
queue<pair<int,int>> q2;
while(q.size()){
pair<int,int> a = q.front();
q.pop();
// cout << a.first << " " << a.second << " " << dist << endl;
if(!bs[a.first]){
bs[a.first] = 1;
for(auto i : v[a.first]){
q.push({a.first,i}) , bs2[a.first][i] = 1;
}
}
if(a.first+a.second < n && !bs2[a.first+a.second][a.second]){
q2.push({a.first+a.second,a.second}) , bs2[a.first+a.second][a.second] = 1;
}
if(a.first-a.second >= 0 && !bs2[a.first-a.second][a.second]){
q2.push({a.first-a.second,a.second}) , bs2[a.first-a.second][a.second] = 1;
}
}
q = q2;
if(bs[tag]){
cout << dist << '\n';
return 0;
}
dist += 1;
}
cout << -1 << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
2 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
2 ms |
1160 KB |
Output is correct |
10 |
Correct |
2 ms |
1384 KB |
Output is correct |
11 |
Correct |
2 ms |
1356 KB |
Output is correct |
12 |
Correct |
2 ms |
1356 KB |
Output is correct |
13 |
Correct |
2 ms |
1356 KB |
Output is correct |
14 |
Correct |
2 ms |
1420 KB |
Output is correct |
15 |
Correct |
2 ms |
1356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1152 KB |
Output is correct |
10 |
Correct |
2 ms |
1356 KB |
Output is correct |
11 |
Correct |
2 ms |
1356 KB |
Output is correct |
12 |
Correct |
2 ms |
1356 KB |
Output is correct |
13 |
Correct |
2 ms |
1356 KB |
Output is correct |
14 |
Correct |
2 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1408 KB |
Output is correct |
16 |
Correct |
2 ms |
972 KB |
Output is correct |
17 |
Correct |
4 ms |
3788 KB |
Output is correct |
18 |
Correct |
2 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
7 ms |
8396 KB |
Output is correct |
21 |
Correct |
2 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
6 ms |
6388 KB |
Output is correct |
24 |
Correct |
8 ms |
7500 KB |
Output is correct |
25 |
Correct |
3 ms |
2508 KB |
Output is correct |
26 |
Correct |
7 ms |
7244 KB |
Output is correct |
27 |
Correct |
7 ms |
7684 KB |
Output is correct |
28 |
Correct |
9 ms |
8408 KB |
Output is correct |
29 |
Correct |
8 ms |
8352 KB |
Output is correct |
30 |
Correct |
6 ms |
8320 KB |
Output is correct |
31 |
Correct |
7 ms |
8332 KB |
Output is correct |
32 |
Correct |
7 ms |
8280 KB |
Output is correct |
33 |
Correct |
10 ms |
8332 KB |
Output is correct |
34 |
Correct |
8 ms |
8396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
2 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
1000 KB |
Output is correct |
8 |
Correct |
1 ms |
1020 KB |
Output is correct |
9 |
Correct |
1 ms |
1152 KB |
Output is correct |
10 |
Correct |
2 ms |
1280 KB |
Output is correct |
11 |
Correct |
2 ms |
1356 KB |
Output is correct |
12 |
Correct |
2 ms |
1356 KB |
Output is correct |
13 |
Correct |
2 ms |
1420 KB |
Output is correct |
14 |
Correct |
2 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
5 ms |
3760 KB |
Output is correct |
18 |
Correct |
2 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
7 ms |
8396 KB |
Output is correct |
21 |
Correct |
2 ms |
972 KB |
Output is correct |
22 |
Correct |
2 ms |
1032 KB |
Output is correct |
23 |
Correct |
5 ms |
6284 KB |
Output is correct |
24 |
Correct |
6 ms |
7500 KB |
Output is correct |
25 |
Correct |
3 ms |
2508 KB |
Output is correct |
26 |
Correct |
6 ms |
7308 KB |
Output is correct |
27 |
Correct |
6 ms |
7716 KB |
Output is correct |
28 |
Correct |
9 ms |
8396 KB |
Output is correct |
29 |
Correct |
8 ms |
8392 KB |
Output is correct |
30 |
Correct |
7 ms |
8268 KB |
Output is correct |
31 |
Correct |
7 ms |
8268 KB |
Output is correct |
32 |
Correct |
7 ms |
8316 KB |
Output is correct |
33 |
Correct |
10 ms |
8320 KB |
Output is correct |
34 |
Correct |
8 ms |
8424 KB |
Output is correct |
35 |
Correct |
12 ms |
6860 KB |
Output is correct |
36 |
Correct |
4 ms |
3468 KB |
Output is correct |
37 |
Correct |
11 ms |
8780 KB |
Output is correct |
38 |
Correct |
14 ms |
9156 KB |
Output is correct |
39 |
Correct |
11 ms |
2636 KB |
Output is correct |
40 |
Correct |
15 ms |
7876 KB |
Output is correct |
41 |
Correct |
18 ms |
9228 KB |
Output is correct |
42 |
Correct |
11 ms |
7756 KB |
Output is correct |
43 |
Correct |
12 ms |
8208 KB |
Output is correct |
44 |
Correct |
12 ms |
8908 KB |
Output is correct |
45 |
Correct |
24 ms |
9164 KB |
Output is correct |
46 |
Correct |
18 ms |
9164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
2 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
2 ms |
1100 KB |
Output is correct |
10 |
Correct |
2 ms |
1356 KB |
Output is correct |
11 |
Correct |
2 ms |
1356 KB |
Output is correct |
12 |
Correct |
2 ms |
1356 KB |
Output is correct |
13 |
Correct |
2 ms |
1356 KB |
Output is correct |
14 |
Correct |
2 ms |
1416 KB |
Output is correct |
15 |
Correct |
2 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
4 ms |
3724 KB |
Output is correct |
18 |
Correct |
2 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
1032 KB |
Output is correct |
20 |
Correct |
9 ms |
8452 KB |
Output is correct |
21 |
Correct |
2 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
5 ms |
6348 KB |
Output is correct |
24 |
Correct |
7 ms |
7500 KB |
Output is correct |
25 |
Correct |
4 ms |
2508 KB |
Output is correct |
26 |
Correct |
7 ms |
7244 KB |
Output is correct |
27 |
Correct |
8 ms |
7756 KB |
Output is correct |
28 |
Correct |
9 ms |
8396 KB |
Output is correct |
29 |
Correct |
8 ms |
8268 KB |
Output is correct |
30 |
Correct |
6 ms |
8268 KB |
Output is correct |
31 |
Correct |
7 ms |
8396 KB |
Output is correct |
32 |
Correct |
7 ms |
8320 KB |
Output is correct |
33 |
Correct |
10 ms |
8380 KB |
Output is correct |
34 |
Correct |
8 ms |
8396 KB |
Output is correct |
35 |
Correct |
12 ms |
6912 KB |
Output is correct |
36 |
Correct |
4 ms |
3532 KB |
Output is correct |
37 |
Correct |
11 ms |
8856 KB |
Output is correct |
38 |
Correct |
15 ms |
9164 KB |
Output is correct |
39 |
Correct |
9 ms |
2568 KB |
Output is correct |
40 |
Correct |
14 ms |
7848 KB |
Output is correct |
41 |
Correct |
17 ms |
9236 KB |
Output is correct |
42 |
Correct |
10 ms |
7680 KB |
Output is correct |
43 |
Correct |
12 ms |
8268 KB |
Output is correct |
44 |
Correct |
12 ms |
8884 KB |
Output is correct |
45 |
Correct |
24 ms |
9164 KB |
Output is correct |
46 |
Correct |
18 ms |
9164 KB |
Output is correct |
47 |
Correct |
35 ms |
40516 KB |
Output is correct |
48 |
Correct |
8 ms |
1740 KB |
Output is correct |
49 |
Correct |
7 ms |
1684 KB |
Output is correct |
50 |
Correct |
6 ms |
1484 KB |
Output is correct |
51 |
Correct |
75 ms |
89408 KB |
Output is correct |
52 |
Correct |
77 ms |
95812 KB |
Output is correct |
53 |
Correct |
22 ms |
18716 KB |
Output is correct |
54 |
Correct |
42 ms |
61124 KB |
Output is correct |
55 |
Correct |
54 ms |
77740 KB |
Output is correct |
56 |
Correct |
87 ms |
112184 KB |
Output is correct |
57 |
Correct |
3 ms |
2764 KB |
Output is correct |
58 |
Correct |
86 ms |
111536 KB |
Output is correct |
59 |
Correct |
83 ms |
103868 KB |
Output is correct |
60 |
Correct |
84 ms |
103868 KB |
Output is correct |
61 |
Correct |
82 ms |
99412 KB |
Output is correct |
62 |
Correct |
151 ms |
112400 KB |
Output is correct |
63 |
Correct |
213 ms |
112008 KB |
Output is correct |
64 |
Correct |
231 ms |
111896 KB |
Output is correct |
65 |
Correct |
260 ms |
112004 KB |
Output is correct |
66 |
Correct |
405 ms |
111916 KB |
Output is correct |
67 |
Correct |
260 ms |
111968 KB |
Output is correct |