#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl ;
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define all(v) (v).begin(),(v).end()
const int MOD = 1000000007;
const int N = 1000005;
const double PI =4*atan(1);
const double eps = 1e-7;
ll n,m;
ll x,y;
vl tab[N];
ll p[N];
ll b[N];
bool vis[N];
bitset<30002> maa[30002];
ll ans=-1;
void bfs(ll u){
queue<pair<ll,ll> > pq;
for(auto t:tab[u]){
pq.push(mp(0,u*30000 + p[t]));
}
vis[u]=1;
pair<ll,ll> ras;
while(!pq.empty()){
ras=pq.front();
pq.pop();
ll x=ras.s/30000;
ll y=ras.s%30000;
if(x==b[1]){
ans=max(ans,ras.f);
continue;
}
ll z;
if(x+y<n) z=maa[x+y][y];
if(x+y <n && z==0){
maa[x+y][y]=1;
if(x+y == b[1]){
ans=max(ans,ras.f+1);
return ;
}
if(vis[x+y]==0){
for(auto t:tab[x+y]){
pq.push(mp(ras.f+1, (x+y)*30000+p[t]));
}
vis[x+y]=1;
}
pq.push(mp(ras.f+1,30000*(x+y)+y));
}
//cout << "trol"<< endl;
if(x-y>=0) z=maa[x-y][y];
if(x-y >=0 && z==0){
maa[x-y][y]=1;
if(x-y == b[1]){
ans=max(ans,ras.f+1);
return;
}
if(vis[x-y]==0){
for(auto t:tab[x-y]){
pq.push(mp(ras.f+1, (x-y)*30000+p[t]));
}
vis[x-y]=1;
}
pq.push(mp(ras.f+1,30000*(x-y)+y));
}
//cout << x << endl;
}
}
int main(){
ios::sync_with_stdio(0);
//freopen("easy.txt","r",stdin);
cin >> n >>m ;
for(int i=0;i<m;i++){
cin >> x >> y;
p[i]=y;
b[i]=x;
tab[x].pb(i);
}
//cout << b[0]<< endl;
bfs(b[0]);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
23 ms |
23800 KB |
Output is correct |
3 |
Correct |
22 ms |
23804 KB |
Output is correct |
4 |
Correct |
24 ms |
23900 KB |
Output is correct |
5 |
Correct |
22 ms |
23892 KB |
Output is correct |
6 |
Correct |
23 ms |
23900 KB |
Output is correct |
7 |
Correct |
26 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
23800 KB |
Output is correct |
3 |
Correct |
23 ms |
23928 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
24 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23928 KB |
Output is correct |
7 |
Correct |
22 ms |
23928 KB |
Output is correct |
8 |
Correct |
23 ms |
23928 KB |
Output is correct |
9 |
Correct |
23 ms |
23928 KB |
Output is correct |
10 |
Correct |
23 ms |
24056 KB |
Output is correct |
11 |
Correct |
23 ms |
24056 KB |
Output is correct |
12 |
Correct |
23 ms |
24184 KB |
Output is correct |
13 |
Correct |
23 ms |
24312 KB |
Output is correct |
14 |
Correct |
24 ms |
24312 KB |
Output is correct |
15 |
Correct |
24 ms |
24312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
22 ms |
23800 KB |
Output is correct |
3 |
Correct |
22 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23928 KB |
Output is correct |
8 |
Correct |
23 ms |
23900 KB |
Output is correct |
9 |
Correct |
23 ms |
23928 KB |
Output is correct |
10 |
Correct |
27 ms |
24056 KB |
Output is correct |
11 |
Correct |
24 ms |
23928 KB |
Output is correct |
12 |
Correct |
23 ms |
24184 KB |
Output is correct |
13 |
Correct |
24 ms |
24312 KB |
Output is correct |
14 |
Correct |
23 ms |
24312 KB |
Output is correct |
15 |
Correct |
23 ms |
24312 KB |
Output is correct |
16 |
Correct |
23 ms |
23928 KB |
Output is correct |
17 |
Correct |
26 ms |
26232 KB |
Output is correct |
18 |
Correct |
23 ms |
23928 KB |
Output is correct |
19 |
Correct |
22 ms |
23900 KB |
Output is correct |
20 |
Correct |
32 ms |
31224 KB |
Output is correct |
21 |
Correct |
23 ms |
23800 KB |
Output is correct |
22 |
Correct |
23 ms |
23928 KB |
Output is correct |
23 |
Correct |
30 ms |
29176 KB |
Output is correct |
24 |
Correct |
31 ms |
30328 KB |
Output is correct |
25 |
Correct |
25 ms |
25208 KB |
Output is correct |
26 |
Correct |
30 ms |
30200 KB |
Output is correct |
27 |
Correct |
31 ms |
30456 KB |
Output is correct |
28 |
Correct |
33 ms |
31304 KB |
Output is correct |
29 |
Correct |
39 ms |
31224 KB |
Output is correct |
30 |
Correct |
32 ms |
31224 KB |
Output is correct |
31 |
Correct |
32 ms |
31224 KB |
Output is correct |
32 |
Correct |
32 ms |
31224 KB |
Output is correct |
33 |
Correct |
38 ms |
31480 KB |
Output is correct |
34 |
Correct |
33 ms |
31224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
23928 KB |
Output is correct |
2 |
Correct |
26 ms |
23800 KB |
Output is correct |
3 |
Correct |
26 ms |
23816 KB |
Output is correct |
4 |
Correct |
27 ms |
23800 KB |
Output is correct |
5 |
Correct |
27 ms |
23928 KB |
Output is correct |
6 |
Correct |
25 ms |
23928 KB |
Output is correct |
7 |
Correct |
27 ms |
23928 KB |
Output is correct |
8 |
Correct |
23 ms |
23804 KB |
Output is correct |
9 |
Correct |
27 ms |
23928 KB |
Output is correct |
10 |
Correct |
23 ms |
24056 KB |
Output is correct |
11 |
Correct |
23 ms |
23980 KB |
Output is correct |
12 |
Correct |
24 ms |
24184 KB |
Output is correct |
13 |
Correct |
23 ms |
24312 KB |
Output is correct |
14 |
Correct |
24 ms |
24312 KB |
Output is correct |
15 |
Correct |
29 ms |
24316 KB |
Output is correct |
16 |
Correct |
27 ms |
23928 KB |
Output is correct |
17 |
Correct |
27 ms |
26360 KB |
Output is correct |
18 |
Correct |
28 ms |
23928 KB |
Output is correct |
19 |
Correct |
23 ms |
23928 KB |
Output is correct |
20 |
Correct |
38 ms |
31352 KB |
Output is correct |
21 |
Correct |
23 ms |
23800 KB |
Output is correct |
22 |
Correct |
28 ms |
23928 KB |
Output is correct |
23 |
Correct |
29 ms |
29176 KB |
Output is correct |
24 |
Correct |
31 ms |
30376 KB |
Output is correct |
25 |
Correct |
25 ms |
25208 KB |
Output is correct |
26 |
Correct |
30 ms |
30200 KB |
Output is correct |
27 |
Correct |
30 ms |
30456 KB |
Output is correct |
28 |
Correct |
33 ms |
31352 KB |
Output is correct |
29 |
Correct |
33 ms |
31224 KB |
Output is correct |
30 |
Correct |
32 ms |
31224 KB |
Output is correct |
31 |
Correct |
32 ms |
31224 KB |
Output is correct |
32 |
Correct |
32 ms |
31224 KB |
Output is correct |
33 |
Correct |
34 ms |
31352 KB |
Output is correct |
34 |
Correct |
34 ms |
31224 KB |
Output is correct |
35 |
Correct |
37 ms |
30328 KB |
Output is correct |
36 |
Correct |
25 ms |
25080 KB |
Output is correct |
37 |
Correct |
33 ms |
28792 KB |
Output is correct |
38 |
Correct |
39 ms |
31100 KB |
Output is correct |
39 |
Correct |
32 ms |
24824 KB |
Output is correct |
40 |
Correct |
32 ms |
24952 KB |
Output is correct |
41 |
Correct |
33 ms |
26104 KB |
Output is correct |
42 |
Correct |
36 ms |
30968 KB |
Output is correct |
43 |
Correct |
37 ms |
31352 KB |
Output is correct |
44 |
Correct |
38 ms |
32412 KB |
Output is correct |
45 |
Correct |
50 ms |
33016 KB |
Output is correct |
46 |
Correct |
44 ms |
33016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23800 KB |
Output is correct |
2 |
Correct |
22 ms |
23804 KB |
Output is correct |
3 |
Correct |
27 ms |
23800 KB |
Output is correct |
4 |
Correct |
25 ms |
23792 KB |
Output is correct |
5 |
Correct |
27 ms |
23928 KB |
Output is correct |
6 |
Correct |
28 ms |
23928 KB |
Output is correct |
7 |
Correct |
23 ms |
23804 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23928 KB |
Output is correct |
10 |
Correct |
23 ms |
24056 KB |
Output is correct |
11 |
Correct |
23 ms |
23928 KB |
Output is correct |
12 |
Correct |
23 ms |
24184 KB |
Output is correct |
13 |
Correct |
24 ms |
24312 KB |
Output is correct |
14 |
Correct |
24 ms |
24312 KB |
Output is correct |
15 |
Correct |
24 ms |
24312 KB |
Output is correct |
16 |
Correct |
23 ms |
23800 KB |
Output is correct |
17 |
Correct |
26 ms |
26232 KB |
Output is correct |
18 |
Correct |
22 ms |
23928 KB |
Output is correct |
19 |
Correct |
27 ms |
23928 KB |
Output is correct |
20 |
Correct |
32 ms |
31352 KB |
Output is correct |
21 |
Correct |
22 ms |
23928 KB |
Output is correct |
22 |
Correct |
22 ms |
23800 KB |
Output is correct |
23 |
Correct |
29 ms |
29176 KB |
Output is correct |
24 |
Correct |
31 ms |
30328 KB |
Output is correct |
25 |
Correct |
25 ms |
25208 KB |
Output is correct |
26 |
Correct |
30 ms |
30200 KB |
Output is correct |
27 |
Correct |
31 ms |
30456 KB |
Output is correct |
28 |
Correct |
33 ms |
31352 KB |
Output is correct |
29 |
Correct |
34 ms |
31196 KB |
Output is correct |
30 |
Correct |
32 ms |
31224 KB |
Output is correct |
31 |
Correct |
32 ms |
31224 KB |
Output is correct |
32 |
Correct |
32 ms |
31224 KB |
Output is correct |
33 |
Correct |
34 ms |
31352 KB |
Output is correct |
34 |
Correct |
33 ms |
31224 KB |
Output is correct |
35 |
Correct |
37 ms |
30200 KB |
Output is correct |
36 |
Correct |
25 ms |
25080 KB |
Output is correct |
37 |
Correct |
33 ms |
28760 KB |
Output is correct |
38 |
Correct |
39 ms |
31096 KB |
Output is correct |
39 |
Correct |
31 ms |
24872 KB |
Output is correct |
40 |
Correct |
32 ms |
25052 KB |
Output is correct |
41 |
Correct |
33 ms |
26104 KB |
Output is correct |
42 |
Correct |
37 ms |
30968 KB |
Output is correct |
43 |
Correct |
37 ms |
31224 KB |
Output is correct |
44 |
Correct |
38 ms |
32412 KB |
Output is correct |
45 |
Correct |
49 ms |
33016 KB |
Output is correct |
46 |
Correct |
44 ms |
32988 KB |
Output is correct |
47 |
Correct |
40 ms |
31736 KB |
Output is correct |
48 |
Correct |
32 ms |
24668 KB |
Output is correct |
49 |
Correct |
31 ms |
24668 KB |
Output is correct |
50 |
Correct |
29 ms |
24444 KB |
Output is correct |
51 |
Correct |
124 ms |
98296 KB |
Output is correct |
52 |
Correct |
144 ms |
107356 KB |
Output is correct |
53 |
Correct |
53 ms |
40868 KB |
Output is correct |
54 |
Correct |
91 ms |
84088 KB |
Output is correct |
55 |
Correct |
112 ms |
100728 KB |
Output is correct |
56 |
Correct |
159 ms |
135416 KB |
Output is correct |
57 |
Correct |
33 ms |
25592 KB |
Output is correct |
58 |
Correct |
167 ms |
135200 KB |
Output is correct |
59 |
Correct |
152 ms |
127096 KB |
Output is correct |
60 |
Correct |
158 ms |
127160 KB |
Output is correct |
61 |
Correct |
144 ms |
121976 KB |
Output is correct |
62 |
Correct |
200 ms |
135564 KB |
Output is correct |
63 |
Correct |
244 ms |
135204 KB |
Output is correct |
64 |
Correct |
262 ms |
135248 KB |
Output is correct |
65 |
Correct |
303 ms |
135532 KB |
Output is correct |
66 |
Correct |
410 ms |
135816 KB |
Output is correct |
67 |
Correct |
282 ms |
135672 KB |
Output is correct |