#include <stdio.h>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int P,N,B[30003],V[30003]; vector<int> G[30003];
int c,u[2][60003],v[2][60003],d[30003];
set<int> chk[30003];
void add(int t, int x)
{
u[t][c] = B[x]; v[t][c] = +V[x]; c++;
u[t][c] = B[x]; v[t][c] = -V[x]; c++;
}
int main()
{
scanf ("%d %d",&P,&N);
for (int i=0;i<P;i++) d[i] = -1;
for (int i=0;i<N;i++){
scanf ("%d %d",&B[i],&V[i]);
G[B[i]].push_back(i);
}
d[B[0]] = 0;
for (int x : G[B[0]]) add(0,x);
for (int p=1;;p++){
int lc = c; c = 0; bool ed = 1;
int pr = (p + 1) % 2, nx = p % 2;
for (int i=0;i<lc;i++){
u[pr][i] += v[pr][i];
if (0 <= u[pr][i] && u[pr][i] < P){
ed = 0;
if (!chk[u[pr][i]].count(abs(v[pr][i]))){
u[nx][c] = u[pr][i]; v[nx][c] = v[pr][i]; c++;
}
if (d[u[pr][i]] == -1){
d[u[pr][i]] = p; ed = 0;
chk[u[pr][i]].insert(abs(v[pr][i]));
for (int x : G[u[pr][i]]){
if (abs(v[pr][i]) != V[x]){
add(nx,x);
chk[u[pr][i]].insert(V[x]);
}
}
}
}
}
if (ed) break;
}
printf ("%d\n",d[B[1]]);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4612 KB |
Output is correct |
2 |
Correct |
0 ms |
4612 KB |
Output is correct |
3 |
Correct |
0 ms |
4612 KB |
Output is correct |
4 |
Correct |
0 ms |
4612 KB |
Output is correct |
5 |
Correct |
0 ms |
4612 KB |
Output is correct |
6 |
Correct |
0 ms |
4612 KB |
Output is correct |
7 |
Correct |
0 ms |
4612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4612 KB |
Output is correct |
2 |
Correct |
0 ms |
4612 KB |
Output is correct |
3 |
Correct |
0 ms |
4612 KB |
Output is correct |
4 |
Correct |
0 ms |
4612 KB |
Output is correct |
5 |
Correct |
0 ms |
4612 KB |
Output is correct |
6 |
Correct |
0 ms |
4612 KB |
Output is correct |
7 |
Correct |
0 ms |
4612 KB |
Output is correct |
8 |
Correct |
0 ms |
4612 KB |
Output is correct |
9 |
Correct |
0 ms |
4612 KB |
Output is correct |
10 |
Correct |
0 ms |
4612 KB |
Output is correct |
11 |
Correct |
0 ms |
4612 KB |
Output is correct |
12 |
Correct |
0 ms |
4612 KB |
Output is correct |
13 |
Correct |
0 ms |
4612 KB |
Output is correct |
14 |
Correct |
0 ms |
4612 KB |
Output is correct |
15 |
Correct |
2 ms |
4612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4612 KB |
Output is correct |
2 |
Correct |
0 ms |
4612 KB |
Output is correct |
3 |
Correct |
0 ms |
4612 KB |
Output is correct |
4 |
Correct |
0 ms |
4612 KB |
Output is correct |
5 |
Correct |
0 ms |
4612 KB |
Output is correct |
6 |
Correct |
0 ms |
4612 KB |
Output is correct |
7 |
Correct |
0 ms |
4612 KB |
Output is correct |
8 |
Correct |
0 ms |
4612 KB |
Output is correct |
9 |
Correct |
0 ms |
4612 KB |
Output is correct |
10 |
Correct |
0 ms |
4612 KB |
Output is correct |
11 |
Correct |
2 ms |
4612 KB |
Output is correct |
12 |
Correct |
0 ms |
4612 KB |
Output is correct |
13 |
Correct |
0 ms |
4612 KB |
Output is correct |
14 |
Correct |
0 ms |
4612 KB |
Output is correct |
15 |
Correct |
0 ms |
4612 KB |
Output is correct |
16 |
Correct |
0 ms |
4612 KB |
Output is correct |
17 |
Correct |
3 ms |
4744 KB |
Output is correct |
18 |
Correct |
0 ms |
4612 KB |
Output is correct |
19 |
Correct |
0 ms |
4612 KB |
Output is correct |
20 |
Correct |
0 ms |
4744 KB |
Output is correct |
21 |
Correct |
0 ms |
4612 KB |
Output is correct |
22 |
Correct |
0 ms |
4612 KB |
Output is correct |
23 |
Correct |
0 ms |
4744 KB |
Output is correct |
24 |
Correct |
1 ms |
4744 KB |
Output is correct |
25 |
Correct |
0 ms |
4744 KB |
Output is correct |
26 |
Correct |
0 ms |
4612 KB |
Output is correct |
27 |
Correct |
0 ms |
4612 KB |
Output is correct |
28 |
Correct |
0 ms |
4744 KB |
Output is correct |
29 |
Correct |
2 ms |
4744 KB |
Output is correct |
30 |
Correct |
0 ms |
4612 KB |
Output is correct |
31 |
Correct |
2 ms |
4612 KB |
Output is correct |
32 |
Correct |
0 ms |
4612 KB |
Output is correct |
33 |
Correct |
0 ms |
4744 KB |
Output is correct |
34 |
Correct |
0 ms |
4744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4612 KB |
Output is correct |
2 |
Correct |
1 ms |
4612 KB |
Output is correct |
3 |
Correct |
0 ms |
4612 KB |
Output is correct |
4 |
Correct |
0 ms |
4612 KB |
Output is correct |
5 |
Correct |
0 ms |
4612 KB |
Output is correct |
6 |
Correct |
0 ms |
4612 KB |
Output is correct |
7 |
Correct |
0 ms |
4612 KB |
Output is correct |
8 |
Correct |
0 ms |
4612 KB |
Output is correct |
9 |
Correct |
0 ms |
4612 KB |
Output is correct |
10 |
Correct |
0 ms |
4612 KB |
Output is correct |
11 |
Correct |
1 ms |
4612 KB |
Output is correct |
12 |
Correct |
0 ms |
4612 KB |
Output is correct |
13 |
Correct |
0 ms |
4612 KB |
Output is correct |
14 |
Correct |
0 ms |
4612 KB |
Output is correct |
15 |
Correct |
0 ms |
4612 KB |
Output is correct |
16 |
Correct |
0 ms |
4612 KB |
Output is correct |
17 |
Correct |
0 ms |
4744 KB |
Output is correct |
18 |
Correct |
0 ms |
4612 KB |
Output is correct |
19 |
Correct |
0 ms |
4612 KB |
Output is correct |
20 |
Correct |
2 ms |
4744 KB |
Output is correct |
21 |
Correct |
0 ms |
4612 KB |
Output is correct |
22 |
Correct |
0 ms |
4612 KB |
Output is correct |
23 |
Correct |
0 ms |
4744 KB |
Output is correct |
24 |
Correct |
0 ms |
4744 KB |
Output is correct |
25 |
Correct |
0 ms |
4744 KB |
Output is correct |
26 |
Correct |
2 ms |
4612 KB |
Output is correct |
27 |
Correct |
0 ms |
4612 KB |
Output is correct |
28 |
Correct |
0 ms |
4744 KB |
Output is correct |
29 |
Correct |
0 ms |
4744 KB |
Output is correct |
30 |
Correct |
0 ms |
4612 KB |
Output is correct |
31 |
Correct |
0 ms |
4612 KB |
Output is correct |
32 |
Correct |
0 ms |
4612 KB |
Output is correct |
33 |
Correct |
4 ms |
4744 KB |
Output is correct |
34 |
Correct |
2 ms |
4744 KB |
Output is correct |
35 |
Correct |
14 ms |
5932 KB |
Output is correct |
36 |
Correct |
4 ms |
4876 KB |
Output is correct |
37 |
Correct |
21 ms |
5668 KB |
Output is correct |
38 |
Correct |
27 ms |
6196 KB |
Output is correct |
39 |
Correct |
15 ms |
6196 KB |
Output is correct |
40 |
Correct |
26 ms |
6196 KB |
Output is correct |
41 |
Correct |
17 ms |
6196 KB |
Output is correct |
42 |
Correct |
0 ms |
4744 KB |
Output is correct |
43 |
Correct |
19 ms |
4744 KB |
Output is correct |
44 |
Correct |
10 ms |
4876 KB |
Output is correct |
45 |
Correct |
23 ms |
6196 KB |
Output is correct |
46 |
Correct |
23 ms |
6196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4612 KB |
Output is correct |
2 |
Correct |
0 ms |
4612 KB |
Output is correct |
3 |
Correct |
0 ms |
4612 KB |
Output is correct |
4 |
Correct |
0 ms |
4612 KB |
Output is correct |
5 |
Correct |
0 ms |
4612 KB |
Output is correct |
6 |
Correct |
0 ms |
4612 KB |
Output is correct |
7 |
Correct |
0 ms |
4612 KB |
Output is correct |
8 |
Correct |
0 ms |
4612 KB |
Output is correct |
9 |
Correct |
0 ms |
4612 KB |
Output is correct |
10 |
Correct |
0 ms |
4612 KB |
Output is correct |
11 |
Correct |
0 ms |
4612 KB |
Output is correct |
12 |
Correct |
0 ms |
4612 KB |
Output is correct |
13 |
Correct |
2 ms |
4612 KB |
Output is correct |
14 |
Correct |
0 ms |
4612 KB |
Output is correct |
15 |
Correct |
0 ms |
4612 KB |
Output is correct |
16 |
Correct |
0 ms |
4612 KB |
Output is correct |
17 |
Correct |
0 ms |
4744 KB |
Output is correct |
18 |
Correct |
0 ms |
4612 KB |
Output is correct |
19 |
Correct |
0 ms |
4612 KB |
Output is correct |
20 |
Correct |
0 ms |
4744 KB |
Output is correct |
21 |
Correct |
0 ms |
4612 KB |
Output is correct |
22 |
Correct |
0 ms |
4612 KB |
Output is correct |
23 |
Correct |
0 ms |
4744 KB |
Output is correct |
24 |
Correct |
0 ms |
4744 KB |
Output is correct |
25 |
Correct |
0 ms |
4744 KB |
Output is correct |
26 |
Correct |
2 ms |
4612 KB |
Output is correct |
27 |
Correct |
3 ms |
4612 KB |
Output is correct |
28 |
Correct |
0 ms |
4744 KB |
Output is correct |
29 |
Correct |
0 ms |
4744 KB |
Output is correct |
30 |
Correct |
0 ms |
4612 KB |
Output is correct |
31 |
Correct |
2 ms |
4612 KB |
Output is correct |
32 |
Correct |
0 ms |
4612 KB |
Output is correct |
33 |
Correct |
4 ms |
4744 KB |
Output is correct |
34 |
Correct |
0 ms |
4744 KB |
Output is correct |
35 |
Correct |
14 ms |
5932 KB |
Output is correct |
36 |
Correct |
0 ms |
4876 KB |
Output is correct |
37 |
Correct |
23 ms |
5668 KB |
Output is correct |
38 |
Correct |
27 ms |
6196 KB |
Output is correct |
39 |
Correct |
20 ms |
6196 KB |
Output is correct |
40 |
Correct |
16 ms |
6196 KB |
Output is correct |
41 |
Correct |
28 ms |
6196 KB |
Output is correct |
42 |
Correct |
9 ms |
4744 KB |
Output is correct |
43 |
Correct |
19 ms |
4744 KB |
Output is correct |
44 |
Correct |
9 ms |
4876 KB |
Output is correct |
45 |
Correct |
21 ms |
6196 KB |
Output is correct |
46 |
Correct |
23 ms |
6196 KB |
Output is correct |
47 |
Correct |
45 ms |
6592 KB |
Output is correct |
48 |
Correct |
6 ms |
5008 KB |
Output is correct |
49 |
Correct |
12 ms |
5008 KB |
Output is correct |
50 |
Correct |
4 ms |
4876 KB |
Output is correct |
51 |
Correct |
37 ms |
7912 KB |
Output is correct |
52 |
Correct |
35 ms |
7912 KB |
Output is correct |
53 |
Correct |
15 ms |
7912 KB |
Output is correct |
54 |
Correct |
2 ms |
5272 KB |
Output is correct |
55 |
Correct |
0 ms |
5536 KB |
Output is correct |
56 |
Correct |
6 ms |
6856 KB |
Output is correct |
57 |
Correct |
22 ms |
6196 KB |
Output is correct |
58 |
Correct |
10 ms |
6064 KB |
Output is correct |
59 |
Correct |
15 ms |
6064 KB |
Output is correct |
60 |
Correct |
25 ms |
6068 KB |
Output is correct |
61 |
Correct |
21 ms |
6212 KB |
Output is correct |
62 |
Correct |
32 ms |
8308 KB |
Output is correct |
63 |
Correct |
67 ms |
7516 KB |
Output is correct |
64 |
Correct |
66 ms |
7516 KB |
Output is correct |
65 |
Correct |
87 ms |
7516 KB |
Output is correct |
66 |
Correct |
148 ms |
7516 KB |
Output is correct |
67 |
Correct |
148 ms |
7516 KB |
Output is correct |