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<vector<int>> ad;
vector<int> dist;
void bfs(int k)
{
dist = vector<int>(2*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.tie(0);
ios_base::sync_with_stdio(0);
cin>>n>>m>>q;
ad = vector<vector<int>>(2*n);
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... |