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>
using namespace std;
typedef int ll;
#define x first
#define y second
#pragma optimize("O3")
vector<vector<ll>> G( 5005);
vector<ll> v[5005];
vector<pair<ll,ll>> input;
void cmc(ll n,ll ini)
{
v[ini].assign(n+1,1e9);
queue<ll> q;
q.push(ini);
v[ini][ini]=0;
map<ll,ll> mapa;
mapa[input[ini].y]=1;
while(!q.empty())
{
ll a=q.front();q.pop();
for(auto i:G[a])
{
if(v[ini][i]==1e9)
{
v[ini][i]=v[ini][a]+1;
if(mapa[input[i].y]==0)
{
q.push(i);
mapa[input[i].y]=1;
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
ll n,q;
cin>>n>>q;
input.push_back({0,0});
vector<pair<ll,ll>> cad;
for(int i=1; i<=n; i++)
{
ll a,b;
cin>>a>>b;
input.push_back({a,b});
cad.push_back({a,i});
cad.push_back({b,i});
}
sort(cad.begin(),cad.end());
set<ll> s;
for(int i=0; i<cad.size(); i++)
{
vector<ll> temp,temp2;
ll a=i;
while(i<cad.size()&&cad[a].x==cad[i].x){
temp.push_back(cad[i].y);
i++;
}
i--;
for(auto j:temp)
{
if(s.find(j)!=s.end())
temp2.push_back(j);
else s.insert(j);
}
for(auto j:temp2) for(auto k:s) G[j].push_back(k);
for(auto j:temp2) if(s.find(j)!=s.end()) s.erase(j);
}
for(int i=1; i<=n; i++)
cmc(n,i);
for(int i=0; i<q; i++)
{
ll a,b;
cin>>a>>b;
a=v[a][b];
if(a==1e9) cout<<"impossible"<<"\n";
else cout<<a<<"\n";
}
return 0;
}
Compilation message (stderr)
events.cpp:6: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
6 | #pragma optimize("O3")
|
events.cpp: In function 'int main()':
events.cpp:53:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i=0; i<cad.size(); i++)
| ~^~~~~~~~~~~
events.cpp:57:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | while(i<cad.size()&&cad[a].x==cad[i].x){
| ~^~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |