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 <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
int n, m, q;
vector<int> ad[10000001];
int dist[10000001];
void bfs(int k)
{
memset(dist, 0, 8*n);
dist[k]=1;
queue<int> q;
q.push(k);
while(!q.empty())
{
int node=q.front();
q.pop();
for(auto& x : ad[node])
{
if(!dist[x])
{
dist[x]=dist[node]+1;
q.push(x);
}
}
}
}
int main()
{
cin>>n>>m>>q;
for(int i=0; i<2*n-1; i++)
{
ad[i].push_back(i+1);
ad[i+1].push_back(i);
}
ad[2*n-1].push_back(0);
ad[0].push_back(2*n-1);
for(int i=0; i<m; i++)
{
int k;
cin>>k;
ad[k].push_back(k+n);
ad[k+n].push_back(k);
}
for(int i=1; i<=q; i++)
{
int x, y;
cin>>x>>y;
bfs(x);
cout<<dist[y]-1<<'\n';
}
}
# | 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... |