///
/// ♪ Pizza mozzarella rella rella rella... ♪
///
#define _USE_MATH_DEFINES
#define FAST ios::sync_with_stdio(false),cin.tie(0);
#include <bits/stdc++.h>
#define Loop(x, l, r) for(int x = (l); x < (r); ++x)
#define LoopR(x, l, r) for(int x = (r)-1; x >= (l); --x)
#define all(x) x.begin(), x.end()
#define Kill(x) exit((cout << (x) << '\n', 0))
#define YN(flag) ((flag)? "YES": "NO")
#define F first
#define S second
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> pii;
typedef std::pair<ll ,ll > pll;
using namespace std;
const int N = 30'010;
const int mod = 50000017;
struct mymap
{
pii* bs = new pii[0];
int v[mod] = {};
unsigned char sz[mod] = {};
int count(int x)
{
int y = x%mod;
if(!sz[y]) return 0;
Loop(i,0,sz[y])
if((bs+v[y])[i].F==x)
return 1;
return 0;
}
int& operator[](int x)
{
int y = x%mod;
if(!sz[y]) v[y] = (new pii[1])-bs, sz[y]=0;
Loop(i,0,sz[y])
if((bs+v[y])[i].F==x)
return (bs+v[y])[i].S;
if(sz[y] && (sz[y]&-sz[y]) == sz[y]){
auto tmp = new pii[sz[y]*2];
Loop(i,0,sz[y]) tmp[i] = (bs+v[y])[i];
delete[](bs+v[y]);
v[y] = tmp-bs;
}
(bs+v[y])[sz[y]++] = {x,0};
return (bs+v[y])[sz[y]-1].S;
}
};
mymap d;
vector<int> P[N];
bool vis[N];
int n, m;
void bfs(int sv, int sp)
{
vector<int> q, q2;
vis[sv] = 1;
for(auto p : P[sv]) if(!d.count(sv<<15^p)) d[sv<<15^p] = 0, q.push_back(sv<<15^p);
if(!d.count(sv<<15^sp)) d[sv<<15^sp] = 0, q.push_back(sv<<15^sp);
for(int d = 0; q.size();)
{
for(ull i = 0; i < q.size(); ++i)
{
int v = q[i]>>15, p = q[i]&32767;
//cerr << v << ' ' << p << ": " << d << '\n';
int u = v+p;
if(0 <= u && u < n)
{
if(!vis[u]){
vis[u] = 1;
for(auto p : P[u]) if(!::d.count(u<<15^p)) ::d[u<<15^p] = d+1, q2.push_back(u<<15^p);
}
if(!::d.count(u<<15^p)) ::d[u<<15^p] = d+1, q2.push_back(u<<15^p);
}
u = v-p;
if(0 <= u && u < n)
{
if(!vis[u]){
vis[u] = 1;
for(auto p : P[u]) if(!::d.count(u<<15^p)) ::d[u<<15^p] = d+1, q2.push_back(u<<15^p);
}
if(!::d.count(u<<15^p)) ::d[u<<15^p] = d+1, q2.push_back(u<<15^p);
}
}
q = q2;
q2.clear();
++d;
}
}
bool isPrime(int n)
{
for(int d = 2; d*d <= n; ++d)
if(n%d == 0)
return 0;
return 1;
}
int main()
{
//Loop(i,50000000,INT_MAX) if(isPrime(i)){cout << i;break;}
FAST;
cin >> n >> m;
int p, src, des, dmy;
cin >> src >> p;
cin >> des >> dmy;
P[des].push_back(dmy);
Loop(i,2,m)
{
int v, p;
cin >> v >> p;
P[v].push_back(p);
}
bfs(src, p);
cout << (d.count(des<<15^dmy)? d[des<<15^dmy]: -1) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
245572 KB |
Output is correct |
2 |
Correct |
104 ms |
245504 KB |
Output is correct |
3 |
Correct |
105 ms |
245564 KB |
Output is correct |
4 |
Correct |
103 ms |
245564 KB |
Output is correct |
5 |
Correct |
105 ms |
245512 KB |
Output is correct |
6 |
Correct |
106 ms |
245632 KB |
Output is correct |
7 |
Correct |
114 ms |
245608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
245596 KB |
Output is correct |
2 |
Correct |
104 ms |
245572 KB |
Output is correct |
3 |
Correct |
106 ms |
245572 KB |
Output is correct |
4 |
Correct |
108 ms |
245584 KB |
Output is correct |
5 |
Correct |
106 ms |
245636 KB |
Output is correct |
6 |
Correct |
107 ms |
245516 KB |
Output is correct |
7 |
Correct |
106 ms |
245516 KB |
Output is correct |
8 |
Correct |
107 ms |
245700 KB |
Output is correct |
9 |
Correct |
120 ms |
245572 KB |
Output is correct |
10 |
Correct |
116 ms |
245636 KB |
Output is correct |
11 |
Correct |
106 ms |
245700 KB |
Output is correct |
12 |
Correct |
106 ms |
245636 KB |
Output is correct |
13 |
Correct |
108 ms |
245548 KB |
Output is correct |
14 |
Correct |
104 ms |
245828 KB |
Output is correct |
15 |
Correct |
105 ms |
245824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
245568 KB |
Output is correct |
2 |
Correct |
105 ms |
245572 KB |
Output is correct |
3 |
Correct |
107 ms |
245512 KB |
Output is correct |
4 |
Correct |
106 ms |
245528 KB |
Output is correct |
5 |
Correct |
106 ms |
245524 KB |
Output is correct |
6 |
Correct |
110 ms |
245592 KB |
Output is correct |
7 |
Correct |
105 ms |
245600 KB |
Output is correct |
8 |
Correct |
104 ms |
245572 KB |
Output is correct |
9 |
Correct |
104 ms |
245512 KB |
Output is correct |
10 |
Correct |
105 ms |
245568 KB |
Output is correct |
11 |
Correct |
104 ms |
245700 KB |
Output is correct |
12 |
Correct |
110 ms |
245640 KB |
Output is correct |
13 |
Correct |
123 ms |
245544 KB |
Output is correct |
14 |
Correct |
104 ms |
245828 KB |
Output is correct |
15 |
Correct |
108 ms |
245772 KB |
Output is correct |
16 |
Correct |
103 ms |
245592 KB |
Output is correct |
17 |
Correct |
107 ms |
246084 KB |
Output is correct |
18 |
Correct |
106 ms |
245700 KB |
Output is correct |
19 |
Correct |
107 ms |
245564 KB |
Output is correct |
20 |
Correct |
105 ms |
245624 KB |
Output is correct |
21 |
Correct |
104 ms |
245568 KB |
Output is correct |
22 |
Correct |
105 ms |
245620 KB |
Output is correct |
23 |
Correct |
108 ms |
245828 KB |
Output is correct |
24 |
Correct |
107 ms |
246060 KB |
Output is correct |
25 |
Correct |
106 ms |
245852 KB |
Output is correct |
26 |
Correct |
109 ms |
245828 KB |
Output is correct |
27 |
Correct |
111 ms |
245872 KB |
Output is correct |
28 |
Correct |
114 ms |
246320 KB |
Output is correct |
29 |
Correct |
120 ms |
247492 KB |
Output is correct |
30 |
Correct |
115 ms |
246076 KB |
Output is correct |
31 |
Correct |
114 ms |
246576 KB |
Output is correct |
32 |
Correct |
111 ms |
246276 KB |
Output is correct |
33 |
Correct |
135 ms |
249408 KB |
Output is correct |
34 |
Correct |
130 ms |
249392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
245544 KB |
Output is correct |
2 |
Correct |
112 ms |
245628 KB |
Output is correct |
3 |
Correct |
103 ms |
245508 KB |
Output is correct |
4 |
Correct |
105 ms |
245616 KB |
Output is correct |
5 |
Correct |
105 ms |
245572 KB |
Output is correct |
6 |
Correct |
104 ms |
245596 KB |
Output is correct |
7 |
Correct |
105 ms |
245572 KB |
Output is correct |
8 |
Correct |
102 ms |
245556 KB |
Output is correct |
9 |
Correct |
107 ms |
245580 KB |
Output is correct |
10 |
Correct |
104 ms |
245572 KB |
Output is correct |
11 |
Correct |
108 ms |
245756 KB |
Output is correct |
12 |
Correct |
107 ms |
245688 KB |
Output is correct |
13 |
Correct |
104 ms |
245700 KB |
Output is correct |
14 |
Correct |
109 ms |
245816 KB |
Output is correct |
15 |
Correct |
106 ms |
245772 KB |
Output is correct |
16 |
Correct |
105 ms |
245572 KB |
Output is correct |
17 |
Correct |
108 ms |
246108 KB |
Output is correct |
18 |
Correct |
104 ms |
245708 KB |
Output is correct |
19 |
Correct |
103 ms |
245540 KB |
Output is correct |
20 |
Correct |
107 ms |
245656 KB |
Output is correct |
21 |
Correct |
112 ms |
245512 KB |
Output is correct |
22 |
Correct |
109 ms |
245700 KB |
Output is correct |
23 |
Correct |
110 ms |
245764 KB |
Output is correct |
24 |
Correct |
107 ms |
246076 KB |
Output is correct |
25 |
Correct |
124 ms |
245864 KB |
Output is correct |
26 |
Correct |
109 ms |
245880 KB |
Output is correct |
27 |
Correct |
106 ms |
245824 KB |
Output is correct |
28 |
Correct |
112 ms |
246340 KB |
Output is correct |
29 |
Correct |
121 ms |
247560 KB |
Output is correct |
30 |
Correct |
111 ms |
246136 KB |
Output is correct |
31 |
Correct |
110 ms |
246596 KB |
Output is correct |
32 |
Correct |
109 ms |
246304 KB |
Output is correct |
33 |
Correct |
128 ms |
249328 KB |
Output is correct |
34 |
Correct |
130 ms |
249404 KB |
Output is correct |
35 |
Correct |
137 ms |
249160 KB |
Output is correct |
36 |
Correct |
110 ms |
246140 KB |
Output is correct |
37 |
Correct |
164 ms |
251268 KB |
Output is correct |
38 |
Correct |
156 ms |
251108 KB |
Output is correct |
39 |
Correct |
153 ms |
251044 KB |
Output is correct |
40 |
Correct |
153 ms |
251124 KB |
Output is correct |
41 |
Correct |
153 ms |
250984 KB |
Output is correct |
42 |
Correct |
114 ms |
245956 KB |
Output is correct |
43 |
Correct |
115 ms |
245840 KB |
Output is correct |
44 |
Correct |
113 ms |
245880 KB |
Output is correct |
45 |
Correct |
213 ms |
261028 KB |
Output is correct |
46 |
Correct |
215 ms |
260984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
245572 KB |
Output is correct |
2 |
Correct |
107 ms |
245572 KB |
Output is correct |
3 |
Correct |
104 ms |
245548 KB |
Output is correct |
4 |
Correct |
107 ms |
245684 KB |
Output is correct |
5 |
Correct |
108 ms |
245636 KB |
Output is correct |
6 |
Correct |
106 ms |
245536 KB |
Output is correct |
7 |
Correct |
106 ms |
245572 KB |
Output is correct |
8 |
Correct |
105 ms |
245576 KB |
Output is correct |
9 |
Correct |
105 ms |
245628 KB |
Output is correct |
10 |
Correct |
107 ms |
245680 KB |
Output is correct |
11 |
Correct |
114 ms |
245728 KB |
Output is correct |
12 |
Correct |
121 ms |
245572 KB |
Output is correct |
13 |
Correct |
104 ms |
245660 KB |
Output is correct |
14 |
Correct |
108 ms |
245760 KB |
Output is correct |
15 |
Correct |
104 ms |
245840 KB |
Output is correct |
16 |
Correct |
117 ms |
245580 KB |
Output is correct |
17 |
Correct |
109 ms |
246080 KB |
Output is correct |
18 |
Correct |
105 ms |
245532 KB |
Output is correct |
19 |
Correct |
106 ms |
245636 KB |
Output is correct |
20 |
Correct |
106 ms |
245632 KB |
Output is correct |
21 |
Correct |
125 ms |
245516 KB |
Output is correct |
22 |
Correct |
101 ms |
245572 KB |
Output is correct |
23 |
Correct |
106 ms |
245828 KB |
Output is correct |
24 |
Correct |
110 ms |
245956 KB |
Output is correct |
25 |
Correct |
108 ms |
245828 KB |
Output is correct |
26 |
Correct |
108 ms |
245864 KB |
Output is correct |
27 |
Correct |
104 ms |
245828 KB |
Output is correct |
28 |
Correct |
113 ms |
246328 KB |
Output is correct |
29 |
Correct |
130 ms |
247556 KB |
Output is correct |
30 |
Correct |
114 ms |
246116 KB |
Output is correct |
31 |
Correct |
113 ms |
246596 KB |
Output is correct |
32 |
Correct |
110 ms |
246388 KB |
Output is correct |
33 |
Correct |
133 ms |
249316 KB |
Output is correct |
34 |
Correct |
129 ms |
249296 KB |
Output is correct |
35 |
Correct |
139 ms |
249292 KB |
Output is correct |
36 |
Correct |
110 ms |
246236 KB |
Output is correct |
37 |
Correct |
154 ms |
251256 KB |
Output is correct |
38 |
Correct |
169 ms |
250996 KB |
Output is correct |
39 |
Correct |
155 ms |
251100 KB |
Output is correct |
40 |
Correct |
155 ms |
251052 KB |
Output is correct |
41 |
Correct |
150 ms |
250988 KB |
Output is correct |
42 |
Correct |
110 ms |
245876 KB |
Output is correct |
43 |
Correct |
126 ms |
245948 KB |
Output is correct |
44 |
Correct |
111 ms |
245788 KB |
Output is correct |
45 |
Correct |
210 ms |
261088 KB |
Output is correct |
46 |
Correct |
206 ms |
261088 KB |
Output is correct |
47 |
Runtime error |
271 ms |
262148 KB |
Execution killed with signal 9 |
48 |
Halted |
0 ms |
0 KB |
- |