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>
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double ld;
int n,m;
int B[30010],P[30010],D[30010],iter[30010],last[30010];
vector<int> adj[30010], doge[30010];
map<pair<int,int>,int> ml,mh;
void addEdges(int b, int p) {
pair<int,int> k={b%p,p};
int lo,hi;
if(ml.find(k)!=ml.end()) lo=ml[k];
else lo=n-1-(n-1-k.fi)%p;
if(mh.find(k)!=mh.end()) hi=mh[k];
else hi=k.fi;
while(lo>b) {
lo-=k.se;
adj[lo].push_back(lo+k.se);
}
while(hi<b) {
hi+=k.se;
adj[hi].push_back(hi-k.se);
}
ml[k]=lo; mh[k]=hi;
}
int main() {
scanf("%d%d",&n,&m);
int i,j;
for(i=0;i<m;i++) {
scanf("%d%d", B+i, P+i);
doge[B[i]].push_back(i);
}
memset(last,-1,sizeof(last));
queue<pair<int,int> > q;
q.push({0,0}); last[0]=0;
int res=-1;
while(!q.empty()) {
int u,w; tie(u,w)=q.front(); q.pop();
if(u==1) { res=w; break; }
if(iter[u]==0) for(auto& it : doge[u]) addEdges(B[it],P[it]);
//printf("u:%d,%d,%d\n",u,iter[u],adj[u].size());
for(int& i=iter[u],it;i<adj[u].size();i++) {
it=adj[u][i];
if(iter[it]==0||(iter[it]<adj[it].size() && iter[it]!=last[it])) {
q.push({it,w+1});
last[it]=iter[it];
}
}
}
printf("%d\n", res);
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:46:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int& i=iter[u],it;i<adj[u].size();i++) {
~^~~~~~~~~~~~~~
skyscraper.cpp:48:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(iter[it]==0||(iter[it]<adj[it].size() && iter[it]!=last[it])) {
~~~~~~~~^~~~~~~~~~~~~~~
skyscraper.cpp:32:8: warning: unused variable 'j' [-Wunused-variable]
int i,j;
^
skyscraper.cpp:31:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
~~~~~^~~~~~~~~~~~~~
skyscraper.cpp:34:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", B+i, P+i);
~~~~~^~~~~~~~~~~~~~~~~~
# | 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... |