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 <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <queue>
#include <string.h>
int n,m,q;
int v[50001];
std::pair<int,int> per[50001];
int cautare(int a,int b)
{
int cnt=0;
std::queue<std::pair<int,int>> co;
co.push({a,0});
while(!co.empty())
{
int x=co.front().first;
int t=co.front().second;
if(x==b)
return t;
int st=x-1,dr=x+1;
if(st<0)
st=2*n-1;
if(dr>n*2-1)
dr=0;
if(x==b)
return cnt;
else
cnt++;
co.pop();
co.push({st,t+1});
co.push({dr,t+1});
if(v[x]!=-1)
{
if(x<n)
co.push({x+n,t+1});
else
co.push({x-n,t+1});
}
}
}
int main()
{
std::cin>>n>>m>>q;
for(int i=1;i<=m;++i)
{
int x;
std::cin>>x;
if(x>=n)
v[x-n]=1;
else
v[x]=1;
}
for(int i=1;i<=q;++i)
std::cin>>per[i].first>>per[i].second;
for(int i=1;i<=q;++i)
{
int x1=per[i].first,y1=per[i].second;
std::cout<<cautare(x1,y1)<<'\n';
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int cautare(int, int)':
Main.cpp:12:36: warning: control reaches end of non-void function [-Wreturn-type]
12 | std::queue<std::pair<int,int>> co;
| ^~
# | 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... |