This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define all(x) begin(x), end(x)
#define SZ(x) (int)(x).size()
#define cps(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int n0 = 30003, inf = 1e9;
int n, m, num, ind[n0];
vector <int> rem[n0], vec[n0], b, d, out[n0];
vector <vector <int>> g;
void bfs(int x) {
deque <int> q;
q.pb(x);
for (int i = 0; i < num; i++)
d[i] = inf;
d[x] = 0;
while (!q.empty()) {
int v = q.front();
q.pop_front();
if (v < n) {
for (int i : out[v]) {
if (d[i] > d[v]) {
d[i] = d[v];
q.push_front(i);
}
}
} else {
for (int i : g[v]) {
if (i < n) {
if (d[i] > d[v]) {
d[i] = d[v];
q.push_front(i);
}
} else {
if (d[i] > d[v] + 1) {
d[i] = d[v] + 1;
q.pb(i);
}
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
cin >> n >> m;
int s, f;
for (int i = 0; i < m; i++) {
int B, P;
cin >> B >> P;
if (i == 0) s = B;
if (i == 1) f = B;
vec[P].pb(B);
rem[P].pb(B % P);
}
num = n;
for (int i = 1; i < n0; i++) {
cps(rem[i]);
for (int j : rem[i])
num += ((n - 1) - j) / i + 1;
}
assert(num < 6000000);
b.resize(num), d.resize(num), g.resize(num);
int cur = n;
for (int i = 1; i < n0; i++) {
for (int j : rem[i]) {
for (int k = j; k < n; k += i) {
if (k > j)
g[cur].pb(cur - 1), g[cur - 1].pb(cur);
g[cur].pb(k);
ind[k] = cur++;
}
}
for (int j : vec[i])
out[j].pb(ind[j]);
}
bfs(s);
if (d[f] == inf) cout << -1;
else cout << d[f];
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:84:9: warning: 'f' may be used uninitialized in this function [-Wmaybe-uninitialized]
84 | if (d[f] == inf) cout << -1;
| ^
skyscraper.cpp:83:5: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
83 | bfs(s);
| ~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |