This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define set unordered_set
#include <iostream>
#include <vector>
#include <set>
using namespace std;
typedef long long ll;
int main() {ios::sync_with_stdio(0); cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n);
vector<int> sta;
vector<int> power;
for (int i = 0; i < m; ++i) {
int bi, pi;
cin >> bi >> pi;
adj[bi].push_back(i);
sta.push_back(bi);
power.push_back(pi);
}
vector<int> seen(n, 0);
set<ll> par;
int amt = 0;
vector<pair<int, int>> st;
vector<pair<int, int>> nex;
st.emplace_back(sta[0], 0);
while (!seen[sta[1]]) {
/*
auto add = [&](ll u, int v) {
if (0 <= u && u < n) {
long long ind = u << 32LL | v;
if (par.find(ind) == par.end()) {
par.insert(ind);
nex.push_back({u, v});
}
}
};
*/
while (!st.empty()) {
auto [loc, p] = st.back();
st.pop_back();
if (!seen[loc]) {
seen[loc] = 1;
for (int u : adj[loc]) {
long long ind = (1LL * loc) << 32LL | (power[u]);
if (par.find(ind) == par.end()) {
par.insert(ind);
st.push_back({loc, power[u]});
}
}
}
ll u = loc + p;
if (0 <= u && u < n) {
long long ind = (u << 32LL) | p;
if (par.find(ind) == par.end()) {
par.insert(ind);
nex.push_back({u, p});
}
}
u = loc - p;
if (0 <= u && u < n) {
long long ind = (u << 32LL) | p;
if (par.find(ind) == par.end()) {
par.insert(ind);
nex.push_back({u, p});
}
}
}
amt += 1;
st.swap(nex);
nex.clear();
if (st.empty() && !seen[sta[1]]) {
cout << -1 << endl;
exit(0);
}
}
cout << amt - 1 << endl;
return 0;
}
Compilation message (stderr)
File "skyscraper.py", line 33
while (!seen[sta[1]]) {
^
SyntaxError: Unknown character
# | 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... |